0

I have DataFrame like below:

df = pd.DataFrame({"day" : ["A", "A", "B", "B"],
                    "data" : ["01.05.2019", "05.02.2019", "10.11.2019", "15.08.2018"]})
df["data"] = df["data"].astype("datetime64")

And I list of datas: list = pd.to_datetime(['01.05.2019','15.08.2018', '25.07.2012'])

And I need to to add to df column = "col1" where:
1 where df"day" = B and df"data" is in "list"
0 otherwise

So I need result like below:

enter image description here

dingaro
  • 2,156
  • 9
  • 29

2 Answers2

0

You can use pandas.DataFrame.apply

df["col1"] = df.apply(lambda x: 1 if x.day == 'B' and x.data in dates else 0, axis = 1)
#  day       data  col1
#0   A 2019-01-05     0
#1   A 2019-05-02     0
#2   B 2019-10-11     0
#3   B 2018-08-15     1

PD: I changed list to dates, please don't use that name.

Pablo C
  • 4,661
  • 2
  • 8
  • 24
0

You can use isin. And also you should add dayfirst=True to to_datetime, otherwise 01.05.2019 will turn into 2019-01-05.

df = pd.DataFrame({"day" : ["A", "A", "B", "B"],
                "data" : ["01.05.2019", "05.02.2019", "10.11.2019", "15.08.2018"]})
df["data"] = pd.to_datetime(df["data"], dayfirst=True)

li = pd.to_datetime(['01.05.2019','15.08.2018', '25.07.2012'], dayfirst=True)

df['col1'] = ((df.day == 'B') & df.data.isin(li)).astype('int')
Emma
  • 8,518
  • 1
  • 18
  • 35
  • there is a mistake because you do not take into consideration that "day" has to be "B" also – dingaro Jan 14 '21 at 23:31
  • Updated. Too early to post it :D – Emma Jan 14 '21 at 23:33
  • Hm I have an erro : ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). – dingaro Jan 14 '21 at 23:43
  • Are you using single `&`? any of these scenario fits your case? https://stackoverflow.com/questions/36921951/truth-value-of-a-series-is-ambiguous-use-a-empty-a-bool-a-item-a-any-o – Emma Jan 14 '21 at 23:48
  • df['col1'] = ((d.dftyg in ['bb, 'cc]) & df.date.isin(list)).astype('int') <-- I am trying this based on your code – dingaro Jan 14 '21 at 23:50
  • `(d.dftyg in ['bb, 'cc])` this should be `isin(['bb', 'cc'])` – Emma Jan 15 '21 at 00:14