52

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?

2 Answers 2

94

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.

Sign up to request clarification or add additional context in comments.

Comments

2

To add to the accepted answer: Remember that you might need to set your timestamp into a datetime!

df = pd.read_csv(dataFile)
df['timestamp'] = pd.to_datetime(df['timestamp'])
df.set_index("timestamp", inplace=True, drop=True)
df.info()

References: https://www.geeksforgeeks.org/python-pandas-dataframe-set_index/ how set column as date index?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.