-1

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?

8885os
  • 65
  • 2
  • 6
  • `.set_index` is not in-place by default – Timus Apr 24 '22 at 14:06
  • when i use inplace=True i get this: https://i.imgur.com/sRkarvr.png – 8885os Apr 24 '22 at 14:09
  • Please help us helping you and provide a MRE (see [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) and [How to make good reproducible pandas examples](https://stackoverflow.com/q/20109391/14311263)). And [Please do not post images of code, data, error messages, etc.](https://stackoverflow.com/help/how-to-ask) – Timus Apr 24 '22 at 14:17
  • Make sure your 'datetime' column is a timestamp – Nev1111 Apr 24 '22 at 14:20
  • @Nev1111 This is actually not relevant (it might be later, but not regarding the question here) – Timus Apr 24 '22 at 14:27

4 Answers4

1

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

tyson.wu
  • 704
  • 5
  • 7
  • Both times i tried this, i got a key error: https://i.imgur.com/sRkarvr.png – 8885os Apr 24 '22 at 14:08
  • 1
    There is another issue in your code: when you `df.filer(['avgLowPrice'])`, the only column left is 'avgLowPrice', and 'datetime' is already filtered out. That's why you have `KeyError` as you see - it means there is no such 'datetime' column recognized in the program. – tyson.wu Apr 24 '22 at 14:13
1

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')
Nev1111
  • 1,039
  • 9
  • 13
0

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

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

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)
nimrobotics
  • 114
  • 1
  • 8