1

I want to filter particular date in a DF column.

My code:

df
df["Crawl Date"]=pd.to_datetime(df["Crawl Date"]).dt.date
date=pd.to_datetime("03-21-2020")
df=df[df["Crawl Date"]==date]

It is showing no match. Note: df column is having time also with date which need to be trimmed.

Thanks in advance.

Amit
  • 327
  • 2
  • 11
  • I tested your code with an example dataframe and it ran alright. Could you print out the content of the "Crawl Date" column to verify that it actually contains dates? – Sheldon Mar 29 '20 at 08:04
  • This code should work correctly. Can you edit it so as to make it reproducible ? See https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples – smarie Mar 29 '20 at 08:05

1 Answers1

0

The following script assumes that the 'Crawl Dates' column contains strings:

import pandas as pd
import datetime

column_names = ["Crawl Date"]
df = pd.DataFrame(columns = column_names)
#Populate dataframe with dates
df.loc[0] = ['03-21-2020 23:45:57']
df.loc[1] = ['03-22-2020 23:12:33']

df["Crawl Date"]=pd.to_datetime(df["Crawl Date"]).dt.date    
date=pd.to_datetime("03-21-2020")
df=df[df["Crawl Date"]==date]

Then df returns:

Crawl Date 0 2020-03-21

Sheldon
  • 4,084
  • 3
  • 20
  • 41