3

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?

Roman
  • 124,451
  • 167
  • 349
  • 456
  • 1
    Can you give an example of what you want? To compute the mean of one columns it is not needed to do a groupby operation: df['col6'].mean() – Wouter Overmeire May 29 '13 at 08:42
  • Mean, was just an example. In general, I would like to have a possibility to group by all columns except the mentioned ones. – Roman May 29 '13 at 09:00
  • It's better provide part of your data as the input, and your expected output. – waitingkuo May 29 '13 at 09:01

1 Answers1

3

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()
Zeugma
  • 31,231
  • 9
  • 69
  • 81