I am new to python (coming from R) and I cannot figure out how to iterate over a data frame in python. I have provided a data frame below and a list of possible "Interventions". What I am attempting to do is search through the "Intervention" column in the data frame and if the intervention is in the "intervention_list" replace the value to "Yes Intervention" but if "NaN" replace with "No Intervention".
Any guidance or help would be appreciated.
import pandas as pd
intervention_list = ['Intervention 1', 'Intervention 2']
df = pd.DataFrame({'ID':[100,200,300,400,500,600,700],
'Intervention':['Intervention 1', 'NaN','NaN','NaN','Intervention 2','Intervention 1','NaN']})
print(df)
I am hoping the finished data frame would look like this:
df_new = pd.DataFrame({'ID':[100,200,300,400,500,600,700],
'Intervention':['Yes Intervention', 'No Intervention','No Intervention','No Intervention','Yes Intervention','Yes Intervention','No Intervention']})
print(df_new)
Thank you!
Aucun commentaire:
Enregistrer un commentaire