pandas 数据框 set_index 未设置

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

Dataframe set_index not setting

pythonpandasdataframe

提问by user2466888

I have a dataframe and am trying to set the index to the column 'Timestamp'. Currently the index is just a row number. An example of Timestamp's format is: 2015-09-03 16:35:00

我有一个数据框,正在尝试将索引设置为“时间戳”列。目前索引只是一个行号。时间戳格式的一个例子是:2015-09-03 16:35:00

I've tried to set the index:

我试图设置索引:

df.set_index('Timestamp')

I don't get an error, but when I print the dataframe, the index is still the row number. How can I use Timestamp as the index?

我没有收到错误消息,但是当我打印数据帧时,索引仍然是行号。如何使用时间戳作为索引?

回答by Batman

You need to either specify inplace=True, or assign the result to a variable. Try:

您需要指定inplace=True或将结果分配给变量。尝试:

df.set_index('Timestamp', inplace=True, drop=True)