I have a dataframe which can be generated using the code given below
data_file= pd.DataFrame({'person_id':[1,1,1,2,2,2,3,3,3],'ob.date': [np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan],
'observation': ['Age','interviewdate','marital_status','Age','interviewdate','marital_status','Age','interviewdate','marital_status'],
'answer': [21,'21/08/2017','Single',26,'11/03/2010','Single',41,'31/09/2012','Married'],
'visit.date': [np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan,np.nan]
})
The input dataframe looks like as shown below
What I would like to do is get the date (interviewdate) value from 'answer' column corresponding to each person and put it in the 'ob.date' and 'visit.date' columns for the same person.
I tried by filtering the dataframe but not sure how to proceed further. This happens only for the filtered rows but I want the dates to be filled in the original or input dataframe
df2 = data_file[(data_file.observation == 'interviewdate')]
df2.reset_index(inplace=True)
df3=data_file.merge(df2)
df3['ob.date']=df2['answer']
df3['visit.date']=df2['answer']
How can I achieve an output to look like as shown below? As you can see each person's interview data is filled in the 'ob.date' and 'visit.date' columns

