我有一個df:
df = pd.DataFrame.from_dict({'category': {1050: 'Dining',
992: 'Dining',
1054: 'Kitchen',
1052: 'Kitchen',
993: 'Living room',
980: 'Living room',
996: 'Dining',
1017: 'Dining',
1020: 'Bath',
1001: 'Bath'},
'subcategory': {1050: 'Chairs',
992: 'Chairs',
1054: 'Stool',
1052: 'Mirror',
993: 'mirror',
980: 'chair',
996: 'Chairs',
1017: 'Chairs',
1020: 'Table',
1001: 'Table'},
'discount': {1050: '30-40',
992: '30-40',
1054: '30-40',
1052: '30-40',
993: '30-40',
980: '30-40',
996: '30-40',
1017: '30-40',
1020: '30-40',
1001: '30-40'},
'sales_1': {1050: 9539.86,
992: 12971.86,
1054: 6736.53,
1052: 7163.16,
993: 8601.16,
980: 8047.16,
996: 16322.0,
1017: 14424.32,
1020: 6319.58,
1001: 4551.42},
'sales_2': {1050: 3226.0,
992: 11117.0,
1054: 1613.0,
1052: 2166.0,
993: 11117.0,
980: 3442.0,
996: 19365.0,
1017: 3323.0,
1020: 1411.0,
1001: 572.0}})
我正在嘗試在多索引中添加小計。我可以像這樣將它添加到 2 個組中:
dd = df_from_dict.groupby(['category', 'subcategory'])[['sales_1', 'sales_2']].sum()
s = dd.groupby(level=0).sum()
s.index = pd.MultiIndex.from_product([s.index, ['Total']])
dd = dd.append(s).sort_index()
dd.loc['Grand Total', :] = dd.sum().values / 2
dd

但是當我將第三個專案添加到組中時, discount
dd = df_from_dict.groupby(['category', 'subcategory','discount'])[['sales_1', 'sales_2']].sum()
s = dd.groupby(level=0).sum()
s.index = pd.MultiIndex.from_product([s.index, ['Total']])
dd = dd.append(s).sort_index()
dd.loc['Grand Total', :] = dd.sum().values / 2
dd
突然間我得到了tuples而不是正常的多索引。我得到的不是 3 個索引,而是 1 個tuple:

而我想要與第一張圖片相同的結構,但具有另一個級別的索引。我嘗試level=1在 group by 中使用引數,但它總是以元組的形式出現在單個索引中,我不確定我的錯誤在哪里。
uj5u.com熱心網友回復:
這里的問題是Series被稱為s是 2 個級別的 MultiIndex,在dd是 3 個級別,因此在append被創建tuple。
解決辦法是設定3個級別MultiIndex中MultiIndex.from_product,因此相同數量的水平像dd和解決作業正確的:
為了避免在DataFrame.sort_indexadd 中對所有其他級別進行排序sort_remaining=False:
dd = df_from_dict.groupby(['category', 'subcategory','discount'])[['sales_1', 'sales_2']].sum()
s = dd.groupby(level=0).sum()
s.index = pd.MultiIndex.from_product([s.index, ['Total'], ['']])
print (s)
dd = dd.append(s).sort_index(level=0, sort_remaining=False)
dd.loc['Grand Total', :] = dd.sum().values / 2
print (dd)
sales_1 sales_2
category subcategory discount
Bath Table 30-40 10871.00 1983.0
Total 10871.00 1983.0
Dining Chairs 30-40 53258.04 37031.0
Total 53258.04 37031.0
Kitchen Mirror 30-40 7163.16 2166.0
Stool 30-40 6736.53 1613.0
Total 13899.69 3779.0
Living room chair 30-40 8047.16 3442.0
mirror 30-40 8601.16 11117.0
Total 16648.32 14559.0
Grand Total 94677.05 57352.0
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/383062.html
上一篇:跨大熊貓資料框應用函式(Kruskal-Wallice)
下一篇:比較熊貓中相同資料幀的時間戳
