数据框 set_index 未设置 [英] Dataframe set_index not setting

查看:26
本文介绍了数据框 set_index 未设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我尝试设置索引:

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?

推荐答案

您需要指定 inplace=True,或者将结果分配给变量.试试:

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

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

基本上,当您设置索引时,您可能想做两件事.一种是 new_df = old_df.set_index('Timestamp', inplace=False).IE.您需要一个具有新索引的新 DataFrame,但仍需要原始 DataFrame 的副本.另一个是df.set_index('Timestamp', inplace=True).用于修改现有对象.

Basically, there are two things that you might want to do when you set the index. One is new_df = old_df.set_index('Timestamp', inplace=False). I.e. You want a new DataFrame that has the new index, but still want a copy of the original DataFrame. The other is df.set_index('Timestamp', inplace=True). Which is for when you want to modify the existing object.

这篇关于数据框 set_index 未设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆