使用 Crosstab 在 Pandas 中聚合具有不同聚合函数的多列
我有一个以下格式的数据框。让我们称之为 df
flag1 | flag2 | type | count1 | count2 |
---|---|---|---|---|
a | x | new | 10 | 2 |
a | y | old | 40 | 5 |
a | x | old | 50 | 6 |
a | y | new | 15 | 1 |
我正在尝试获取以下格式。 (我无法合并count1和count2的相邻单元格)
count1 | count2 | ||||
---|---|---|---|---|---|
new | old | new | old | ||
a | x | 10 | 50 | 2 | 6 |
a | y | 15 | 40 | 1 | 5 |
当我必须仅对一列(count1)进行聚合时,我尝试了以下操作,并且以下操作有效:
pd.crosstab([df.flag1,df.flag2], df.type, values=df.count1, aggfunc='sum')
但由于我想要两列数据, count1 和 count2,我尝试了以下方法,但没有成功,但
pd.crosstab([df.flag1,df.flag2], df.type, values=[df.count1,df.count2], aggfunc=['sum','sum']) #trial1
pd.crosstab([df.flag1,df.flag2], df.type, values=[df.count1,df.count2], aggfunc='sum') #trial2
没有一个有效。
扩展:我应该能够在不同的列上使用不同的功能。说对 count1 求和,对 count2 求和 或 对 count1 求和,对 count2 求平均值
I have a dataframe of the below format. Let us call it df
flag1 | flag2 | type | count1 | count2 |
---|---|---|---|---|
a | x | new | 10 | 2 |
a | y | old | 40 | 5 |
a | x | old | 50 | 6 |
a | y | new | 15 | 1 |
I am trying to get the following format. (I could not merge the adjacent cells of count1 and count2)
count1 | count2 | ||||
---|---|---|---|---|---|
new | old | new | old | ||
a | x | 10 | 50 | 2 | 6 |
a | y | 15 | 40 | 1 | 5 |
I tried the following when i had to do the aggregate on only one column (count1) and the following worked:
pd.crosstab([df.flag1,df.flag2], df.type, values=df.count1, aggfunc='sum')
But since i want two columns of data, both count1 and count2, I tried the following but did not work out
pd.crosstab([df.flag1,df.flag2], df.type, values=[df.count1,df.count2], aggfunc=['sum','sum']) #trial1
pd.crosstab([df.flag1,df.flag2], df.type, values=[df.count1,df.count2], aggfunc='sum') #trial2
None of them worked.
Extension : I should be able use different functions on the different columns. say sum on count1 and nunique on count2 or sum on count1 and mean on count2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为
crosstab
在这里不可能使用,替代方法是DataFrame.pivot_table
:另一种聚合替代方案:
I think
crosstab
is not possible use here, alternative isDataFrame.pivot_table
:Another alternative with aggregation: