Dataframe set_index not setting
Question:
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?
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.
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?
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?
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.
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?