convert pandas dataframe to dictionary with multiple keys

Viewed 2727

I am trying to convert a data frame to dictionary with four keys, which are all from columns. I also have multiple columns that I want to return values from using keys built from those four columns. I worked on a way with loops but end up in memory error. I am curious that is there any more efficient way for this?

The data frame looks like this:

    Service Bill Weight Zone    Resi    UPS FedEx   USPS    DHL
    1DEA           1       2    N      33.02    9999    9999    9999
    1DEA           2       2    N      33.02    9999    9999    9999
    1DEA           3       2    N      33.02    9999    9999    9999

I want to have a key for each of the carriers like this:

    price[('1DEA', '1', '2', 'N', 'UPS')]=33.02
    price[('1DEA', '1', '2', 'N', 'FedEx')]=9999

I have tried this:

    price = {}
    carriers = ['UPS', 'FedEx', 'USPS','DHL'] 
    for carrier in carriers:
        for row in rate_keys.to_dict('records'):
              key = (row['Service'], row['Bill Weight'], row['Zone'], 
              row['Resi'], carrier)
              rate_keys[key] = row[carrier]
5 Answers

Set the index to be all but the carrier columns, then stack.

df.set_index(['Service', 'Bill Weight', 'Zone', 'Resi']).stack().to_dict()

{('1DEA', 1, 2, 'N', 'DHL'): 9999.0,
 ('1DEA', 1, 2, 'N', 'FedEx'): 9999.0,
 ('1DEA', 1, 2, 'N', 'UPS'): 33.02,
 ('1DEA', 1, 2, 'N', 'USPS'): 9999.0,
 ('1DEA', 2, 2, 'N', 'DHL'): 9999.0,
 ('1DEA', 2, 2, 'N', 'FedEx'): 9999.0,
 ('1DEA', 2, 2, 'N', 'UPS'): 33.02,
 ('1DEA', 2, 2, 'N', 'USPS'): 9999.0,
 ('1DEA', 3, 2, 'N', 'DHL'): 9999.0,
 ('1DEA', 3, 2, 'N', 'FedEx'): 9999.0,
 ('1DEA', 3, 2, 'N', 'UPS'): 33.02,
 ('1DEA', 3, 2, 'N', 'USPS'): 9999.0}

Comprehension

{(*r[:4], c): v for r in df.values for c, v in zip(df.columns[4:], r[4:])}

{('1DEA', 1, 2, 'N', 'DHL'): 9999,
 ('1DEA', 1, 2, 'N', 'FedEx'): 9999,
 ('1DEA', 1, 2, 'N', 'UPS'): 33.02,
 ('1DEA', 1, 2, 'N', 'USPS'): 9999,
 ('1DEA', 2, 2, 'N', 'DHL'): 9999,
 ('1DEA', 2, 2, 'N', 'FedEx'): 9999,
 ('1DEA', 2, 2, 'N', 'UPS'): 33.02,
 ('1DEA', 2, 2, 'N', 'USPS'): 9999,
 ('1DEA', 3, 2, 'N', 'DHL'): 9999,
 ('1DEA', 3, 2, 'N', 'FedEx'): 9999,
 ('1DEA', 3, 2, 'N', 'UPS'): 33.02,
 ('1DEA', 3, 2, 'N', 'USPS'): 9999}

IIUC, with a list comprehension like this:

carriers = ['UPS', 'FedEx', 'USPS','DHL']
price = {(row['Service'], row['Bill Weight'], row['Zone'], row['Resi'], c):row[c]
     for c in carriers for _, row in df.iterrows()}

[output]

{('1DEA', 1, 2, 'N', 'UPS'): 33.02,
 ('1DEA', 2, 2, 'N', 'UPS'): 33.02,
 ('1DEA', 3, 2, 'N', 'UPS'): 33.02,
 ('1DEA', 1, 2, 'N', 'FedEx'): 9999,
 ('1DEA', 2, 2, 'N', 'FedEx'): 9999,
 ('1DEA', 3, 2, 'N', 'FedEx'): 9999,
 ('1DEA', 1, 2, 'N', 'USPS'): 9999,
 ('1DEA', 2, 2, 'N', 'USPS'): 9999,
 ('1DEA', 3, 2, 'N', 'USPS'): 9999,
 ('1DEA', 1, 2, 'N', 'DHL'): 9999,
 ('1DEA', 2, 2, 'N', 'DHL'): 9999,
 ('1DEA', 3, 2, 'N', 'DHL'): 9999}

You probably shouldn't update rate_keys while looping on it. I guess the last line of your example script should read

price[key] = row[carrier]

if you do

df = df.set_index(['Service', 'Bill','Weight','Zone'])

you essentially have the same thing

output

print(df.loc[('1DEA', 1, 2, 'N')]['UPS'])

9999.0

Firstly,

temp = df.set_index(['Service', 'Bill', 'Weight', 'Zone']).to_dict()

Then, we do a dictionary comprehension to get the desired output,

dict(((k+(i,)), a[i][k]) for i in temp for (k) in temp[i] )
Related