gpt4 book ai didi

python - 在 Pandas 中如何从另一列的观察和聚合值创建新列

转载 作者:行者123 更新时间:2023-12-01 22:54:59 25 4
gpt4 key购买 nike

我有这个数据框,我想将它转换成另一个数据框,其中包含一个列,该列结合了第一个数据框中多个列的观察结果,并聚合了“点”列中的值。这是数据框,下面是所需的结果:

player_data = pd.DataFrame({"customer_id": ["100001", "100002", "100005", "100006", "100007", "100011", "100012", 
"100013", "100022", "100023", "100025", "100028", "100029", "100030"],
"country": ["Austria", "Germany", "Germany", "Sweden", "Sweden", "Austria", "Sweden",
"Austria", "Germany", "Germany", "Austria", "Austria", "Germany", "Austria"],
"category": ["basic", "pro", "basic", "advanced", "pro", "intermidiate", "pro",
"basic", "intermidiate", "intermidiate", "advanced", "basic", "intermidiate", "basic"],
"gender": ["male", "male", "female", "female", "female", "male", "female",
"female", "male", "male", "female", "male", "male", "male"],
"age_group": ["20", "30", "20", "30", "40", "20", "40",
"20", "30", "30", "40", "20", "30", "20"],
"points": [200, 480, 180, 330, 440, 240, 520, 180, 320, 300, 320, 200, 280, 180]})

The new dataframe is supposed to look like this:

谢谢大家!

最佳答案

这就是您要找的东西吗?

df_new = df.groupby(['country', 'category', 'gender', 'age_group'])['points'].agg('sum').reset_index()
df_new.pivot_table(values = 'points', index = ['country', 'category', 'gender'], columns = 'age_group', fill_value = 0).reset_index().sort_values(['country', 'category', 'gender'])

但是,这不会有任何仅包含 0 的列,例如 Australia |进阶 | M 不会出现在这里,因为其中没有提及原始 df。如果您想动态添加它们,您可能需要重新考虑 df 的结构。

关于python - 在 Pandas 中如何从另一列的观察和聚合值创建新列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73474807/

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