将pandas数据帧转换为具有多个键的字典

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将pandas数据帧转换为具有多个键的字典相关的知识,希望对你有一定的参考价值。

我试图将数据框转换为具有四个键的字典,这四个键都来自列。我还有多个列,我想使用从这四列构建的键返回值。我在循环方式上工作但最终导致内存错误。我很好奇,有没有更有效的方法呢?

数据框如下所示:

    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]
答案

您可能不应该在循环时更新rate_keys。我想你的示例脚本的最后一行应该是读

price[key] = row[carrier]
另一答案

将索引设置为除载波列之外的所有索引,然后堆叠。

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}

理解

{(*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,列表理解如下:

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()}

[输出]

{('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}
另一答案

如果你这样做

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

你基本上有同样的事情

output

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

9999.0
另一答案

首先,

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

然后,我们进行字典理解以获得所需的输出,

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

以上是关于将pandas数据帧转换为具有多个键的字典的主要内容,如果未能解决你的问题,请参考以下文章

具有多个键的熊猫数据框到 json

python pandas将数据框转换为具有多个值的字典

如何将带有元组键的 python 字典转换为 pandas 多索引数据框?

Pandas:如何将具有重复索引值的数据框转换为字典

将 dict 构造函数转换为 Pandas MultiIndex 数据帧

如何将嵌套字典转换为 pandas DataFrame?