2
我有两个数据框,其中包含客户ID(标记为“C_ID”)和一年的访问次数。如果ID在其他数据框中存在,Python Pandas数据框在新列中添加“1”
我想在我的2010数据框中添加一列,如果客户在2009年也逛过所以我需要创建一个循环检查,如果从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
这是完美的 - 你是一个天才!谢谢 – jeangelj
@jeangelj,你可以接受答案,如果它的工作。谢谢你:) – Vaishali
我已经接受它并向上投票 – jeangelj