2017-04-09 30 views
2

我將2個CSV文件與pandas python進行比較,所有工作都正常。它是在EMPLOYEE_ID列匹配,並輸出結果到CSV文件Python Pandas - 未找到匹配項時返回默認值

df1 = pd.read_csv('input1.csv', sep=',\s+', delimiter=',', encoding="utf-8") 
    df2 = pd.read_csv('input2.csv', sep=',\s,', delimiter=',', encoding="utf-8") 
    df3 = pd.merge(df1,df2, on='employee_id', how='right') 
    df3.to_csv('output.csv', encoding='utf-8', index=False) 

,如果未找到匹配它返回一個空的結果,我想它,而不是返回NOT_FOUND

這可能是熊貓還是應該在事後做一些處理?

回答

2

我認爲你可以使用fillna如果沒有NaNdf1

df1 = pd.DataFrame({'employee_id':[1,2,3], 
        'B':[4,5,6], 
        'C':[7,8,9]}) 

print (df1) 
    B C employee_id 
0 4 7   1 
1 5 8   2 
2 6 9   3 

df2 = pd.DataFrame({'employee_id':[1,4,6], 
        'D':[4,5,6], 
        'E':[7,8,9]}) 

print (df2) 
    D E employee_id 
0 4 7   1 
1 5 8   4 
2 6 9   6 

df3 = pd.merge(df1,df2, on='employee_id', how='right') 
df3[df1.columns] = df3[df1.columns].fillna('not_found') 
print (df3) 
      B   C employee_id D E 
0   4   7   1 4 7 
1 not_found not_found   4 5 8 
2 not_found not_found   6 6 9 

但如果NaNdf1需要創造面具從right標識缺失值加入 - 在merge與參數indicator=Trueisin和否定面膜通過~

df1 = pd.DataFrame({'employee_id':[1,2,3], 
        'B':[np.nan,5,6], 
        'C':[7,8,9]}) 

print (df1) 
    B C employee_id 
0 NaN 7   1 
1 5.0 8   2 
2 6.0 9   3 

df3 = pd.merge(df1,df2, on='employee_id', how='right', indicator=True) 
mask = df3['_merge'] == 'right_only' 
df3.loc[mask, df1.columns.difference(['employee_id'])] = 
df3.loc[mask,df1.columns.difference(['employee_id'])].fillna('not_found') 

df3 = df3.drop('_merge', axis=1) 
print (df3) 
      B   C employee_id D E 
0  NaN   7   1 4 7 
1 not_found not_found   4 5 8 
2 not_found not_found   6 6 9 

df3 = pd.merge(df1,df2, on='employee_id', how='right') 
mask = ~df2['employee_id'].isin(df1['employee_id']) 

df3.loc[mask, df1.columns.difference(['employee_id'])] = \ 
df3.loc[mask,df1.columns.difference(['employee_id'])].fillna('not_found') 

print (df3) 
      B   C employee_id D E 
0  NaN   7   1 4 7 
1 not_found not_found   4 5 8 
2 not_found not_found   6 6 9 
+0

非常感謝你,他們是很好的例子。保存我不得不處理文件後 – fightstarr20

+0

很高興能幫助你;)美好的一天! – jezrael