Is there in pandas operation complementary (opposite) to groupby?

我的未来我决定 提交于 2019-12-23 18:25:25

问题


I have a table (data frame) with many columns. Now I would like to average values in one of the columns. It means that I need to group by over all columns except the one over which I need to average. Of course I can write:

df.groupby(['col1', 'col2', 'col3', 'col4', 'col5'])['vals'].mean()

But it would be nice if I could do something like:

df.groupby(['col6'], something='reverse')['vals'].mean()

Is it possible in pandas?


回答1:


You are searching for the complementary columns to a list you have on hands. You can play with df.columns. It represents an Index object that allows some interesting manipulations.

df.columns.drop(['col6']) returns an Index with the list of columns passed as argument removed. You can convert it into a list and use it as the groupby argument:

df.groupby(df.columns.drop(['col6']).tolist())['vals'].mean()


来源:https://stackoverflow.com/questions/16808682/is-there-in-pandas-operation-complementary-opposite-to-groupby

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