Issue
I have a csv that looks like
AccountExternalID Customer
1 RogerInc
2 FredLLC
I am turning that into a Pandas DF, and I want to turn that into a dict that looks like
{'RogerInc': 1, 'FredLLC': 2}
This is what I tried;
def build_custid_dict(csv_path: str=None) -> dict[str]:
csv_path = r'\\path\CustomerIDs.csv'
df = pd.read_csv(csv_path)
# Strip whitespace
df[df.columns] = df.apply(lambda x: x.str.strip())
df_dict = df.to_dict('list')
return df_dict
Solution
Example
data = {'AccountExternalID': {0: 1, 1: 2}, 'Customer': {0: 'RogerInc', 1: 'FredLLC'}}
df = pd.DataFrame(data)
output(df
):
AccountExternalID Customer
0 1 RogerInc
1 2 FredLLC
Code
use following code in your func:
dict(df.iloc[:, [1, 0]].values)
result:
{'RogerInc': 1, 'FredLLC': 2}
Answered By - Panda Kim
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.