2017-02-06 36 views
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 

回答

4

您可以使用dataframe.isin()

% timeit df_2010['new'] = np.where(df_2010['C_ID'].isin(df_2009['C_ID']), 1, 0) 

最好的3:每圈384微秒

由於@Kris建議

%timeit df_2010['new'] = (df_2010['C_ID'].isin(df_2009['C_ID'])).astype(int) 

最好的3:每圈584微秒

注意

df_2010['new'] = df_2010['C_ID'].isin(df_2009['C_ID']) 

也將工作,但新的列將具有值true和false就地分別爲1和0。

+0

這是完美的 - 你是一個天才!謝謝 – jeangelj

+0

@jeangelj,你可以接受答案,如果它的工作。謝謝你:) – Vaishali

+0

我已經接受它並向上投票 – jeangelj

相關問題