【发布时间】:2019-02-11 00:21:51
【问题描述】:
我正在尝试将数据框转换为具有四个键的字典,这些键都来自列。我还有多个列,我想使用从这四列构建的键返回值。我使用循环的方式工作,但最终出现内存错误。我很好奇有没有更有效的方法呢?
数据框如下所示:
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
我希望每个运营商都有一个这样的密钥:
price[('1DEA', '1', '2', 'N', 'UPS')]=33.02
price[('1DEA', '1', '2', 'N', 'FedEx')]=9999
我试过这个:
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]
【问题讨论】:
标签: pandas loops dictionary key jupyter