我有一个多索引,我正在尝试将小计添加到它的不同级别上。
这是数据框
id date_created country animals count
32 2019-05-26 Australia chicken 1
32 2019-05-26 Australia chicken 1
32 2019-05-19 Australia chicken 1
32 2019-05-12 Australia chicken 1
32 2019-05-12 Australia cow 1
32 2019-05-12 United Kingdom cow 1
32 2019-05-12 United Kingdom cow 1
data = df.groupby([
pd.Grouper(key='id'),
pd.Grouper(key='date_created', freq='W-SUN'), 'country', 'animals'
]).count()
data = data.unstack('animals').sort_values(by=['id', 'date_created',ascending=False)
id date_created country chickens cows
32 2019-05-26 Australia 2 0
United Kingdom 0 0
2019-05-19 Australia 1 0
United Kingdom 0 0
2019-05-12 Australia 1 1
United Kingdom 0 2
我尝试了这个答案,但无法正常工作:
Python (Pandas) Add subtotal on each lvl of multiindex dataframe
它只是添加重复的行
pd.concat([
data.assign(
**{x: '' for x in 'id'[i:]}
).groupby(data.index.names).sum() for i in range(1, 4)
]).sort_index()
这就是我想要的样子
id date_created country chickens cows 32 2019-05-26 Australia 2 0 United Kingdom 0 0 subtotal 2 0 2019-05-19 Australia 1 0 United Kingdom 0 0 subtotal 1 0 2019-05-12 Australia 1 1 United Kingdom 0 2 subtotal 1 3 grand total 4 3