pandas pivot_table 的边距仅垂直聚合 [英] pandas pivot_table's margins is only aggregating vertically
问题描述
考虑一个数据框:
df = pd.DataFrame(
{'last_year': [1, 2, 3], 'next_year': [4, 5, 6]},
index=['foo', 'bar', 'star']
)
last_year next_year
foo 1 4
bar 2 5
star 3 6
我正在寻找一种简单的方法来显示此表周围的总计,包括列和行.
我的想法是通过 .pivot_table()
抛出它:
My thought was to throw it through .pivot_table()
:
pd.pivot_table(
df,
index=df.index,
margins=True,
aggfunc=sum
)
然而,这只适用于第一个轴(垂直):
However, this only works for the first axis (vertically):
last_year next_year
bar 2 5
foo 1 4
star 3 6
All 6 15
我错过了什么?为什么不计算行明智的总数,就像在 本文档的示例?另外,为什么会打乱我的索引顺序?
What did I miss? How come no row wise totals are calculated as well, just like in this documentation's example? Also, why is it messing up my index' order?
我对 df['All'] = df.sum(axis=1)
类型的解决方案不感兴趣;我想要一种不影响我原始数据框的动态方法.数据透视表似乎是最合乎逻辑的方法(据我所知),但也许有更好的方法!
I'm not interested in df['All'] = df.sum(axis=1)
kind of solutions; I want a dynamic approach which does not affect my original dataframe. A pivot table seems like the most logical way to do this (as far as I know) but maybe there are better ways!
推荐答案
我的猜测是 pivot_table
中的每一列都是它自己的组,所以你看不到水平聚合(这是毫无意义的聚合一个元素).为了演示差异,请考虑 stack()
:
My guess is that each column is its own group in pivot_table
so you don't see the horizontal aggregation (it's rather pointless to aggregate one single element). To demonstrate the difference, consider stack()
:
(df.stack().reset_index(name='value')
.pivot_table(index='level_0', columns='level_1', values='value', margins=True,
aggfunc='sum')
)
输出:
level_1 last_year next_year All
level_0
bar 2 5 7
foo 1 4 5
star 3 6 9
All 6 15 21
也就是说,这很可能是一个错误.
That said, it might very well be a bug.
这篇关于pandas pivot_table 的边距仅垂直聚合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!