2017-02-06 37 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

相关问题