Closed
Description
related #7000
In [1]: df = DataFrame({'foo1' : ['one', 'two', 'two', 'three', 'one', 'two'],
'foo2' : np.random.randn(6)})
In [2]: df
Out[2]:
foo1 foo2
0 one 1.006666
1 two 0.002063
2 two 1.507785
3 three 1.865921
4 one 0.141202
5 two -1.079792
[6 rows x 2 columns]
In [3]: df.groupby('foo1').mean()
Out[3]:
foo2
foo1
one 0.573934
three 1.865921
two 0.143352
[3 rows x 1 columns]
In [4]: df.groupby('foo1').apply(lambda x: x.mean())
Out[4]:
foo2
foo1
one 0.573934
three 1.865921
two 0.143352
[3 rows x 1 columns]
This should return the foo1 column as well
[6]: df.groupby('foo1',as_index=False).apply(lambda x: x.mean())
Out[6]:
foo2
0 0.573934
1 1.865921
2 0.143352
[3 rows x 1 columns]
In [7]: df.groupby('foo1',as_index=False).mean()
Out[7]:
foo1 foo2
0 one 0.573934
1 three 1.865921
2 two 0.143352
[3 rows x 2 columns]