Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 340
user2466888 Avatar asked Feb 13 '17 03:02

user2466888


People also ask

Which does the set_index () method do?

The set_index() function is used to set the DataFrame index using existing columns. Set the DataFrame index (row labels) using one or more existing columns or arrays of the correct length. The index can replace the existing index or expand on it.

How do you set a column to index in pandas?

To create an index, from a column, in Pandas dataframe you use the set_index() method. For example, if you want the column “Year” to be index you type <code>df. set_index(“Year”)</code>. Now, the set_index() method will return the modified dataframe as a result.


1 Answers

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.

like image 148
Batman Avatar answered Sep 21 '22 08:09

Batman