pandas 如何删除熊猫数据透视表中的多级索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44513488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove multilevel index in pandas pivot table
提问by Shivpe_R
I have a dataframe as given:
我有一个给定的数据框:
df = {'TYPE' : pd.Series(['Advisory','Advisory1','Advisory2','Advisory3']),
'CNTRY' : pd.Series(['IND','FRN','IND','FRN']),
'VALUE' : pd.Series([1., 2., 3., 4.])}
df = pd.DataFrame(df)
df = pd.pivot_table(df,index=["CNTRY"],columns=["TYPE"]).reset_index()
After pivoting, how can I get the dataframe having columns and df
to be like the below; removing the multilevel index, VALUE
旋转后,如何获得具有列的数据框并df
如下所示;删除多级索引,VALUE
Type|CNTRY|Advisory|Advisory1|Advisory2|Advisory3
0 FRN NaN 2.0 NaN 4.0
1 IND 1.0 NaN 3.0 NaN
回答by jezrael
You can add parameter values
:
您可以添加参数values
:
df = pd.pivot_table(df,index="CNTRY",columns="TYPE", values='VALUE').reset_index()
print (df)
TYPE CNTRY Advisory Advisory1 Advisory2 Advisory3
0 FRN NaN 2.0 NaN 4.0
1 IND 1.0 NaN 3.0 NaN
And for remove columns name rename_axis
:
对于删除列名称rename_axis
:
df = pd.pivot_table(df,index="CNTRY",columns="TYPE", values='VALUE') \
.reset_index().rename_axis(None, axis=1)
print (df)
CNTRY Advisory Advisory1 Advisory2 Advisory3
0 FRN NaN 2.0 NaN 4.0
1 IND 1.0 NaN 3.0 NaN
But maybe is necessary only pivot
:
但也许只是必要的pivot
:
df = df.pivot(index="CNTRY",columns="TYPE", values='VALUE') \
.reset_index().rename_axis(None, axis=1)
print (df)
CNTRY Advisory Advisory1 Advisory2 Advisory3
0 FRN NaN 2.0 NaN 4.0
1 IND 1.0 NaN 3.0 NaN
because pivot_table
aggregate duplicates by default aggregate function mean
:
因为pivot_table
默认聚合函数聚合重复mean
:
df = {'TYPE' : pd.Series(['Advisory','Advisory1','Advisory2','Advisory1']),
'CNTRY' : pd.Series(['IND','FRN','IND','FRN']),
'VALUE' : pd.Series([1., 4., 3., 4.])}
df = pd.DataFrame(df)
print (df)
CNTRY TYPE VALUE
0 IND Advisory 1.0
1 FRN Advisory1 1.0 <-same FRN and Advisory1
2 IND Advisory2 3.0
3 FRN Advisory1 4.0 <-same FRN and Advisory1
df = df.pivot_table(index="CNTRY",columns="TYPE", values='VALUE')
.reset_index().rename_axis(None, axis=1)
print (df)
TYPE Advisory Advisory1 Advisory2
CNTRY
FRN 0.0 2.5 0.0
IND 1.0 0.0 3.0
Alternative with groupby
, aggregate function and unstack
:
替代groupby
, 聚合函数 和unstack
:
df = df.groupby(["CNTRY","TYPE"])['VALUE'].mean().unstack(fill_value=0)
.reset_index().rename_axis(None, axis=1)
print (df)
CNTRY Advisory Advisory1 Advisory2
0 FRN 0.0 2.5 0.0
1 IND 1.0 0.0 3.0
回答by piRSquared
You can use set_index
with unstack
你可以用set_index
与unstack
df.set_index(['CNTRY', 'TYPE']).VALUE.unstack().reset_index()
TYPE CNTRY Advisory Advisory1 Advisory2 Advisory3
0 FRN NaN 2.0 NaN 4.0
1 IND 1.0 NaN 3.0 NaN