Dataframe set_index not setting
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
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?
Solution 1:
You need to either specify inplace=True
, or assign the result to a variable. Try:
df.set_index('Timestamp', inplace=True, drop=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.