Python 将索引转换为列熊猫数据框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44773714/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 00:26:23  来源:igfitidea点击:

convert index to column pandas dataframe

pythonpandasdataframe

提问by Safariba

I have following pandas dataframe:

我有以下熊猫数据框:

                    |     id    |  LocTime        |ZPos   | XPos
datetime            |               
2017-01-02 00:14:39 |20421902611|   12531245409231| 0     | -6              
2017-01-02 00:14:40 |30453291020|   28332479673070| 0     | -2  

I want to convert datetime index to column of the data frame. I tried df.reset_index(level=['datetime'])but the result does not change. any idea?

我想将日期时间索引转换为数据框的列。我试过了,df.reset_index(level=['datetime'])但结果没有改变。任何的想法?

回答by jezrael

Need assign output back or inplace=Trueparameter:

需要分配输出或inplace=True参数:

df = df.reset_index()


df.reset_index(inplace=True)


print (df)
              datetime           id         LocalTime  ZPosition  XPosition
0  2017-01-02 00:14:39  10453190861  1483312478909238          0         -9
1  2017-01-02 00:14:40  10453191020  1483312479673076          0         -8