What am I doing wrong turning Pandas DF to dict?

Question:

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'\pathCustomerIDs.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
Asked By: DuckButts

||

Answers:

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

The to_dict method will map the column headers to the column data. You don’t want that for what you’re trying to do.

Instead, you can do something like this:

ids = df['AccountExternalID'].values
customers = df['Customer'].values
df_dict = {customer:id for customer, id in zip(customers, ids)}
Answered By: cmauck10

Try this one you can select one column of the dataframe as the key and another one as the value of dict.

def build_custid_dict(csv_path: str=None) -> dict[str]:
    csv_path = r'\pathCustomerIDs.csv'
    df = pd.read_csv(csv_path)
    # Strip whitespace
    df['AccountExternalID'].str.strip()
    df['Customer'].str.strip()
    df = df.set_index('AccountExternalID') 
    df_dict = df.to_dict('Customer')
    dict((v, k) for k, v in df_dict.items()) #reversing the keys and values
    return df_dict

Also refer to this Stack overflow question

Answered By: shashank2806

You could also use the to_dict method on a series, e.g.

df.set_index('Customer').AccountExternalID.to_dict()
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.