I am having trouble selecting rows from a dataframe with a datetime column from a CSV file. I have my code and my sample data. There are no rows returned.
import pandas as pd
import numpy as np
col_names = ['date','msft','nok','aapl','ibm','amzn']
stockprices = pd.read_csv('./stockdata.csv', skiprows=1, names=col_names)
stockprices.loc['2018-01-01 9:00:00':'2018-01-01 11:00:00']
CSV file data:
date msft nok aapl ibm amzn
2018-01-01 08:00:00 107 2 161 140 1295
2018-01-01 09:00:00 112 1 143 130 1298
2018-01-01 10:00:00 109 10 185 137 1647
2018-01-01 11:00:00 98 11 146 105 1331
2018-01-01 12:00:00 83 3 214 131 1355
Basically, trying to select the 3 rows with 9:00:00, 10:00:00 and 11:00:00 times in the dataframe.
Is using the .loc the best way to do this?
Thank you.
If you have multiple dates
you can get it by converting to DateTime and filter
df["date"]=pd.to_datetime(df["date"])
df[df["date"].between('2018-01-01 09:00:00','2018-01-01 11:00:00')]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With