[英]MultiIndex on both Columns and Rows
我嘗試使用pd.MultiIndex但它只顯示類似此屏幕截圖的內容
還有其他方法可以得到此結果嗎? 這是我使用的代碼:
df1 = pd.DataFrame.merge(sad,das, how='outer')
df1.set_index(['parent_category','category','name'], inplace= True)
df1.fillna(value=0)
df1
header = pd.MultiIndex.from_product([['January','February','March','April','May','June','July'],['Purchased','Sold','Inventory']], names = ['months','header'])
df2 = pd.DataFrame(df1,columns=header)
df2
您可以為df1 columns
分配新的列名稱,但原始列和Multiindex的長度必須相同:
df1 = pd.merge(sad,das, how='outer') \
.set_index(['parent_category','category','name']) \
.fillna(value=0)
months = ['January','February','March','April','May','June','July']
items = ['Purchased','Sold','Inventory']
len(pd.MultiIndex.from_product([months,items], names = ['months','header']))
21
len(df1.columns)
df1.columns = pd.MultiIndex.from_product([months,items], names = ['months','header'])
df1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.