I have a large dataframes with date column like this one:
import pandas as pd
cars = {'Date': ['2020-09-11','2020-10-11','2021-01-12','2020-01-03', '2021-02-01'],
'Brand': ['Honda Civic','Toyota Corolla','Ford Focus','Audi A4','Mercedes'],
'Price': [22000,25000,27000,35000,45000]
}
df = pd.DataFrame(cars, columns = ['Date','Brand', 'Price'])
print (df)
Now the date column is an object and I want to filter out only 2021 data. Originally I am changing date column from object to datetime but this approach wont work if I try to convert that data to json format. This what I tried
yest_date = date(2021, 1, 01) - timedelta(days=1)
yest_date = yest_date.strftime("%Y-%m-%d")
cur_date = date(2021, 2, 05)
cur_date = cur_date.strftime("%Y-%m-%d")
df['Date] = pd.to_datetime(df['Date'])
if yest_date is not None:
yest_data = df.loc[df['Date'] <= pd.to_datetime(yest_date), :]
if cur_date is not None:
cur_data = data2.loc[data2[project.date_var] <= pd.to_datetime(cur_date), :]
My question not is, can I be able to filter out values between 2 dates without converting the date object date to datetime? If so How can filter date between 2 dates without changing date structure to datetime.
Treat the Date column as string and do a substring match with year, like 2021
Here I am using str.contains
import pandas as pd
cars = {'Date': ['2020-09-11','2020-10-11','2021-01-12','2020-01-03', '2021-02-01'],
'Brand': ['Honda Civic','Toyota Corolla','Ford Focus','Audi A4','Mercedes'],
'Price': [22000,25000,27000,35000,45000]
}
df = pd.DataFrame(cars, columns = ['Date','Brand', 'Price'])
df = df[df['Date'].str.contains("2021")]
df.head()
Date Brand Price
2 2021-01-12 Ford Focus 27000
4 2021-02-01 Mercedes 45000
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