Suppose we have some measurement for each city each year:
City Year Income
1 NYC 2000 1.1
2 NYC 2001 2.4
3 NYC 2002 3.4
...
12 London 2000 1.2
13 London 2001 1.5
...
pd.groupby('City')
City Year Income
NYC 2000 1.1
2001 2.4
2002 3.4
London 2000 1.2
2001 1.5
Pairs 2000 3.2
2001 1.31
2002 2.2
Now I know the Year for each city should be [2000,2001,2002]. How can I add missing rows? Here, London doesn't have 2002. So I want to achieve:
City Year Income
NYC 2000 1.1
2001 2.4
2002 3.4
London 2000 1.2
2001 1.5
2002 NA
Pairs 2000 3.2
2001 1.31
2002 2.2
df.groupby(['City','Year']).sum().unstack('Year').stack('Year', dropna=False).