Pandas Multiindex Groupby on Columns

大憨熊 提交于 2019-12-05 00:32:00

问题


Is there anyway to use groupby on the columns in a Multiindex. I know you can on the rows and there is good documentation in that regard. However I cannot seem to groupby on columns. The only solution I have is transposing the dataframe.

#generate data (copied from pandas example)
arrays=[['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]
tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])
df = pd.DataFrame(np.random.randn(3, 8), index=['A', 'B', 'C'], columns=index)

Now I will try to groupby columns which fails

df.groupby(level=1)
df.groupby(level='first')

However transposing with rows works

df.T.groupby(level=1)
df.T.groupby(level='first')

So is there a way to do this without transposing?


回答1:


You need to specify the axis in the groupby method:

df.groupby(level = 1, axis = 1).sum()

Or if you mean groupby level 0:

df.groupby(level = 0, axis = 1).sum()



来源:https://stackoverflow.com/questions/40745981/pandas-multiindex-groupby-on-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!