3

enter image description here

I am new to python and trying to make a small program with python. My requirement : I have a list like this: holiday = ['1 January 2018','26 January 2018','2 March 2018','30 March 2018']

and I have a pandas DataFrame which print like this: enter image description here

Now I want to search for the Dates in dataframe which match with my list like "26 January 2018" and want to save that search result in a separate data frame.

I am not getting the proper way to do this. pls suggest

Arpit Solanki
  • 9,567
  • 3
  • 41
  • 57
Jagdish
  • 67
  • 6

2 Answers2

2

1. String matching

If your date formats match, you can directly go for string matching (although it's a crude way)

df2 = df[df['date'].isin(holiday)]

2. Parsing datetime (proper way)

Parse the dates first and then go for the match!

a. Parse the df dates

df['date'] = pd.to_datetime(df['date'], format='%d %B %Y')

b. Parse the dates in the list

import datetime as dt
holiday = ['1 January 2018','26 January 2018','2 March 2018','30 March 2018']
holiday_parsed = [dt.strptime(i, format='%d %B %Y') for i in holiday]

And then do the filtering -

df2 = df[df['date'].isin(holiday_parsed)]

The .isin() is a pandas convenience method that lets you search your pandas series with a list.

Hope that helps!

Vivek Kalyanarangan
  • 8,951
  • 1
  • 23
  • 42
  • 1
    Thanks for your help. I could also find an one more easy way to filter like this: holiday=df[df["date"].isin(['1 January 2018','26 January 2018','2 March 2018','30 March 2018','15 August 2018','22 August 2018', '2 October 2018','19 October 2018', '7 November 2018','8 November 2018','9 November 2018','25 December 2018'])] – Jagdish Feb 07 '18 at 16:04
  • @Jagdish what you did basically the same thing. I just put the list in a variable and you put the list directly – Vivek Kalyanarangan Feb 08 '18 at 05:22
  • Thanks Vivek, I got it. Can you please help me on a another issue posted here : https://stackoverflow.com/q/48668501/9325592 – Jagdish Feb 08 '18 at 05:26
1

You can use DataFrame.query method

holiday = ['1 January 2018','26 January 2018','2 March 2018','30 March 2018']
df.query('date==@holiday')
Arpit Solanki
  • 9,567
  • 3
  • 41
  • 57