The new index is the new group you made with groupby()
. The ['y']
will return the column y
. But, you also need to call a function on your aggregated rows, like sum()
. Here's an example:
import pandas as pd
df = pd.DataFrame({'Name':['Mark', 'Laura', 'Adam', 'Roger', 'Anna'],
'City':['Lisbon', 'Montreal', 'Lisbon', 'Berlin', 'Glasgow'],
'Height':[173.4, 151.8, 179.3, 169.1, 166.4]})
print(df)
Name City Height
0 Mark Lisbon 173.4
1 Laura Montreal 151.8
2 Adam Lisbon 179.3
3 Roger Berlin 169.1
4 Anna Glasgow 166.4
Return the sum of the people, grouped by the City
:
df.groupby('City').sum()['Height']
Out[46]:
City
Berlin 169.1
Glasgow 166.4
Lisbon 352.7
Montreal 151.8
Name: Height, dtype: float64
The new index is the group, and you selected one column to display. You can either put it before or after sum()
.