gpt4 book ai didi

python - 将 pandas 数据帧映射到具有组合键的字典

转载 作者:行者123 更新时间:2023-12-01 00:19:49 24 4
gpt4 key购买 nike

我正在编写一些代码,需要将 pandas 数据帧映射到由复合键和某个值组成的字典中。下面是一个起始示例,(PostalCode, Sex)(Name, Age)组成,value > 与key 匹配的所有salary 的总和。我正在寻找一种优雅地完成此映射的方法。

import pandas as pd

data = [
["tom", 22, "ab 11", "M", 5555],
["Rob", 22, "ab 13", "M", 9999],
["nick", 33, "ab 14", "M", 3333],
["nick", 33, "ab 14", "M", 8888],
["juli", 18, "ab 15", "F", 2222],
]
people = pd.DataFrame(data, columns=["Name", "Age", "PostalCode", "Sex", "Salary"])

df = people.groupby(["PostalCode", "Sex", "Age"])["Salary"].sum().unstack(0)

d = {col: df[col].dropna().to_dict() for col in df}

print(d)

# Expected output
print(
{
(("ab 11", "M"), ("tom", 22)): 5555,
(("ab 13", "M"), ("Rob", 22)): 9999,
(("ab 14", "M"), ("nick", 33)): 12221,
(("ab 15", "F"), ("juli", 18)): 2222,
}
)

最佳答案

首先聚合sum,然后在字典理解中更改MultiIndex中值的格式,并将键解包为变量a,b,c,d :

s = people.groupby(["PostalCode", "Sex","Name", "Age"])["Salary"].sum()
print (s)
PostalCode Sex Name Age
ab 11 M tom 22 5555
ab 13 M Rob 22 9999
ab 14 M nick 33 12221
ab 15 F juli 18 2222
Name: Salary, dtype: int64

d= {((a,b), (c,d)): v for (a,b,c,d), v in s.items()}
print(d)
{(('ab 11', 'M'), ('tom', 22)): 5555,
(('ab 13', 'M'), ('Rob', 22)): 9999,
(('ab 14', 'M'), ('nick', 33)): 12221,
(('ab 15', 'F'), ('juli', 18)): 2222}

关于python - 将 pandas 数据帧映射到具有组合键的字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59031275/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com