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