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]