0

This is my list:

my_list = [
2002-01-11 22:15:00, 
2002-02-12 10:30:00, 
2002-03-14 02:30:00, 
2002-04-12 22:15:00
]

I have DataFrame:

                 dt_object         diff
0      2002-01-01 00:00:00   -160.95041
1      2002-01-01 00:15:00   -160.81016
2      2002-01-01 00:30:00   -160.66989
3      2002-01-01 00:45:00   -160.52961
4      2002-01-01 01:00:00   -160.38930

I want to create new column 'Hit' with False value by default and True value when dates from list match.

Expected output:

                 dt_object         diff   hit
0      2002-01-01 00:00:00   -160.95041   False
1      2002-01-01 00:15:00   -160.81016   False
2      2002-01-01 00:30:00   -160.66989   False
3      2002-01-01 00:45:00   -160.52961   False
4      2002-01-01 01:00:00   -160.38930   False
....................
....................
1010      2002-01-11 22:15:00   -150.54678   True

because 2002-01-11 22:15:00 is in list.

2 Answers 2

1

you can do:

import numpy as np
df['hit'] = np.where(df['dt_object'].isin(my_list),1,0)) # will give 1 or 0 according if the condition is satisfied.

To just get back True or False, just remove the returning part.

df['hit'] = df['dt_object'].isin(my_list)
Sign up to request clarification or add additional context in comments.

1 Comment

you don't need numpy where in second option
1

Use Series.isin

df['hit'] = df['dt_object'].isin(my_list)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.