【发布时间】:2019-07-08 03:07:36
【问题描述】:
我是 python 新手(来自 R),我不知道如何在 python 中迭代数据框。我在下面提供了一个数据框和一个可能的“干预”列表。我试图做的是搜索数据框中的“干预”列,如果干预在“intervention_list”中,则将值替换为“是干预”,但如果“NaN”替换为“无干预”。
任何指导或帮助将不胜感激。
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)
我希望完成的数据框如下所示:
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)
谢谢!
【问题讨论】:
标签: python pandas loops for-loop if-statement