gpt4 book ai didi

python - Groupby Pandas 抛出 ValueError : Grouper and axis must be same length

转载 作者:行者123 更新时间:2023-12-04 10:07:58 25 4
gpt4 key购买 nike

我有下面的csv

ID,PR_No,PMO,PRO,REV,COST
111,111,AB,MA,2575,2575
111,111,AB,MA,-1137,-1137
112,112,CD,KB,1134,3334
111,111,AB,MA,100,100

输出如下
ID,PR_No,PMO,PRO,REV,COST
111,111,AB,MA,1538,1538
112,112,CD,KB,1134,3334

1538=2575-1137+100

我的代码抛出值错误
df_n = df.groupby([['ID','PR_No','PMO','PRO']]).agg({'REV':sum,'COST':sum})

最佳答案

删除嵌套 []对于列名称列表:

df_n = df.groupby(['ID','PR_No','PMO','PRO']).agg({'REV':sum,'COST':sum})
print (df_n)
REV COST
ID PR_No PMO PRO
111 111 AB MA 1538 1538
112 112 CD KB 1134 3334

因为相同的聚合函数可以在 groupby 之后使用 .sum 创建列表:
df_n = df.groupby(['ID','PR_No','PMO','PRO'])['REV','COST'].sum()

ValueError: Grouper and axis must be same length



这是什么意思?

如果使用示例数据运行它可以工作,因为嵌套列表的长度与示例数据的长度相同:
df_n = df.groupby([['ID','PR_No','PMO','PRO']]).agg({'REV':sum,'COST':sum})
print (df_n)
REV COST
ID 2575 2575
PMO 1134 3334
PRO 100 100
PR_No -1137 -1137

如果创建具有重复项的列表,它会聚合示例数据:
df_n = df.groupby([['ID','ID','PRO','PRO']]).agg({'REV':sum,'COST':sum})
print (df_n)
REV COST
ID 1438 1438
PRO 1234 3434

但如果长度不同,则失败:
print (df)
ID PR_No PMO PRO REV COST
0 111 111 AB MA 2575 2575
1 111 111 AB MA -1137 -1137
2 112 112 CD KB 1134 3334
3 111 111 AB MA 100 100
4 111 111 AB MA 100 100 <- added new row

df_n = df.groupby([['ID','ID','PRO','PRO']]).agg({'REV':sum,'COST':sum})
print (df_n)

ValueError: Grouper and axis must be same length

关于python - Groupby Pandas 抛出 ValueError : Grouper and axis must be same length,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61472850/

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