【发布时间】:2017-06-23 22:16:35
【问题描述】:
我有两个数据框,其中包含客户 ID(标记为“C_ID”)和一年的访问次数。
如果客户也在 2009 年购物,我想在 2010 年的数据框中添加一列。所以我需要创建一个循环检查 2010 年的 C_ID 是否存在于 2009 年,添加 1,否则添加 0。
我使用了这段代码,但没有工作:(没有错误消息,没有任何反应)
for row in df_2010.iterrows():
#check if C_ID exists in the other dataframe
check = df_2009[(df_2009['C_ID'] == row['C_ID'])]
if check.empty:
#ID not exist in 2009 file, add 0 in new column
row['shopped2009'] = 0
else:
#ID exists in 2009 file, add 1 into same column
row['shopped2009'] = 1
【问题讨论】:
标签: python loops pandas dataframe