johnboy
johnboy

Reputation: 111

Pandas Dataframe New Column: if x in list

I am creating new columns for a df but I would like to include an if statement while doing so. My df is just a column of dates.

      date
2020-01-01
2020-01-02
       ...

I want to check if a key is in a list. If yes the df[col] = 1, else 0. The key is equal to str(x + y + date)

for x in list1:

    for y in list2:

        df[col] = if str(x + y + df['date']) in keylist: 1, else: 0

What would be the best way to structure this? Thank you!

Upvotes: 0

Views: 745

Answers (1)

cmosig
cmosig

Reputation: 1317

you need to reorder your if statement. This should work:

df[col] = 1 if str(x + y + df['date']) in keylist else 0

Check out this post.

Upvotes: 1

Related Questions