将 Pandas 数据透视表转换为常规数据框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43756052/
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
transform pandas pivot table to regular dataframe
提问by DougKruger
How can I convert a pandas pivot table to a regular dataframe ? For example:
如何将 Pandas 数据透视表转换为常规数据框?例如:
amount
categories A B C
date deposit
2017-01-15 6220140.00 5614354.16 0.00 0.00
2017-01-16 7384354.00 6247300.22 0.00 0.00
2017-01-17 6783939.00 10630021.37 0.00 0.00
2017-01-18 67940.00 4659384.47 0.00 0.00
to a regular datetime such as this:
到一个常规的日期时间,例如:
date deposit A B C
0 2017-01-15 6220140.00 5614354.16 0.00 0.00
1 2017-01-16 7384354.00 6247300.22 0.00 0.00
2 2017-01-17 6783939.00 10630021.37 0.00 0.00
3 2017-01-18 67940.00 4659384.47 0.00 0.00
回答by jezrael
Use droplevel
+ index name
to None
+ reset_index
:
使用droplevel
+index name
到None
+ reset_index
:
df.columns = df.columns.droplevel(0) #remove amount
df.columns.name = None #remove categories
df = df.reset_index() #index to columns
Alternatively use rename_axis
:
或者使用rename_axis
:
df.columns = df.columns.droplevel(0)
df = df.reset_index().rename_axis(None, axis=1)
EDIT:
编辑:
Maybe also help remove []
in parameter values
- see this.
也许也有助于[]
在参数中删除values
- 请参阅this。