2016-02-11 42 views
2

之間我有2個非索引數據幀,具有完全相同的數據結構(同一列),爲簡單起見在這裏是一個例子:大熊貓:數據差(△)2個數據幀

DF1

0  1  2   3 4 
JoeK Joe  Kavanagh 120 [email protected] 
BarryD Barry Dempsy  11 [email protected] 
OrlaF Orla Farrel  236 [email protected] 
SethB Seth Black  563 [email protected] 
KateW Kate White  254 [email protected] 

,第二個:

DF2

0  1  2   3 4 
JoeK Joe  Kavanagh 110 [email protected] 
BarryD Barry Dempsy  11 [email protected] 
JimmyS Jimmy Smith  250 [email protected] 
SethB Seth Blake  563 [email protected] 

我想有一個RESU lting表示DF1不在DF2行dataframes:

0  1  2   3 4 
JoeK Joe  Kavanagh 120 [email protected] 
OrlaF Orla Farrel  236 [email protected] 
SethB Seth Black  563 [email protected] 
KateW Kate White  254 [email protected] 

注意,第一行(Joek,和SethB)在兩個dataframes存在,但第3列的用於JoeK值和列的值2的SethB已經改變了,這就是他們進入最終結果集的原因。

任何幫助,非常感謝。

+0

你的結果不應該只是'df1',因爲電子郵件不符合第二行嗎?或者你的限制只是在第0-3列上匹配? – EdChum

+0

那麼斑點,那是一個錯字。我現在糾正了它。 –

回答

1

IIUC它看起來像你只是想在第0,1,2,3列上匹配,你可以執行左邊的merge,如果你使用的是最新版本的熊貓,然後過濾掉df,你可以通過參數indicator=True

In [197]: 
merged = df1.merge(df2, how='left', on=['0','1','2','3'],indicator=True) 
merged[merged['_merge'] == 'left_only'] 

Out[197]: 
     0  1   2 3      4_x 4_y  _merge 
0 JoeK Joe Kavanagh 120 [email protected] NaN left_only 
2 OrlaF Orla Farrel 236   [email protected] NaN left_only 
3 SethB Seth  Black 563  [email protected] NaN left_only 
4 KateW Kate  White 254   [email protected] NaN left_only 
+0

我想在這裏的所有列上匹配。那是一個樣本,我擁有60個以上的數據框。所以我會使用on = df1.columns.tolist() –

+0

默認情況下它會匹配所有列,所以在你的情況下,除非你有不同的要求,否則你不需要傳遞'on'參數值 – EdChum

+0

這應該工作' merged = df1.merge(df2,how ='left',indicator = True)'我想,你也可以upvote – EdChum