Pandas: If string exists in one of the columns, copy the date to a new column

I have the following dataframe that contains all strings: Current Dataframe

I would like to add a Column (“Accepted Date”) that captures the date from any comment that contains the words “Now Accepted” like the following:

Desired Dataframe

I appreciate any help on this.

Answer

Try something like this:

from datetime import datetime
import dateutil.parser as dparser

df= pd.DataFrame({'col1':[1,2,3],'cmnt1':["7/23/2021; bla bla Now Accepted", np.nan, "7/23/2021; updated"],'cmnt2':[np.nan,"7/25/2021; bla bla Now Accepted","7/29/2021; bla bla Now Accepted"]})

df['Accept_Date']=np.nan
for y in ['cmnt1','cmnt2']:
    df['Accept_Date'].update(df[y][df[y].str.contains("Now Accepted", na=False)])
    
df['Accept_Date'].update(df.Accept_Date.apply(lambda x: dparser.parse(x,fuzzy=True).strftime('%m/%d/%Y')))

Output:

col1 cmnt1 cmnt2 Accept_Date
1 7/23/2021; bla bla Now Accepted NaN 07/23/2021
2 NaN 7/25/2021; bla bla Now Accepted 07/25/2021
3 7/23/2021; updated 7/29/2021; bla bla Now Accepted 07/29/2021