问题
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