Home > Blockchain >  Set index as datetime: pandas, python
Set index as datetime: pandas, python

Time:04-24

I have an original dataset which looks like this:enter image description here

I am trying to take the avgLowPrice out aswell as the datetime and have the dates as the index and a column of avgLowPrice. This is my code:

df=pd.read_csv('data.csv')
data = df.filter(['avgLowPrice'])
data.set_index(df['datetime'])

and when i print data i get this: enter image description here So instead of my data being indexed by datetime it is just indexed by numbers 0-300 how do i convert 0-300 into my original datetime column to have the price indexed by datetime?

CodePudding user response:

You should have either data = data.set_index(df['datetime']) or data.set_index(df['datetime'], inplace=True). See the documentation: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.set_index.html

CodePudding user response:

Make sure your 'datetime' column is a timestamp first, df['datetime']=pd.to_datetime(df['datetime']) Then set it as the index

df=df.set_index('datetime')

CodePudding user response:

you can filter with data[['datetime','avgLowPrice']] then data.set_index('datetime')

data = data[['datetime','avgLowPrice']]
data = data.set_index('datetime')

CodePudding user response:

You need to convert your datetime column to datetime object first, then set the index using this column.

df['time'] = pd.to_datetime(df['datetime'], format='%m/%d/%Y %H:%M')
df['time'] = df['time'].dt.strftime('%m/%d/%Y %H:%M')
df.set_index('time', inplace=True)
  • Related