2016-06-24 10 views
3

NaNで行を検索せずに、2つの外部マージデータフレームと内部マージデータフレームの違いを見つけたいと思います。 differenceメソッドを使用してこれを行う方法がありますか、できればFrameAFrameBの両方を作成する必要はありませんか?Merge(Outer-Inner)Pandas DFの差異を見つける

import pandas as pd 

DataA = pd.DataFrame([{"a": 1, "b": 4}, {"a": 6, "b": 2}, {"a": 2, "b": 5}, {"a": 3, "b": 6}, {"a": 7, "b": 2}]) 
DataB = pd.DataFrame([{"a": 2, "d": 7}, {"a": 7, "d": 8}, {"a": 3, "d": 8}]) 

DATAA

a b 
0 1 4 
1 6 2 
2 2 5 
3 3 6 
4 7 2 

DATAB

a d 
0 2 7 
1 7 8 
2 3 8 

...

FrameA = pd.merge(DataA, DataB, on = "a", how ='inner') 
FrameB = pd.merge(DataA, DataB, on = "a", how ='outer') 

FrameA

a b d 
0 2 5 7 
1 3 6 8 
2 7 2 8 

FrameBデータフレームの違いを見つけようとして

a b d 
0 1 4 NaN 
1 6 2 NaN 
2 2 5 7 
3 3 6 8 
4 7 2 8 

...

list(FrameB.index.difference(FrameA.index)) 

たぶん、あなたはこの所望の出力とのより良い解決策を持っている:

a b d 
0 1 4 NaN 
1 6 2 NaN 

答えて

4

あなたが探していますsymmetric_difference

a = DataA.set_index('a') 
b = DataB.set_index('a') 

# select rows from the outer join using the symmetric difference (^) 
a.join(b, how='outer').loc[a.index^b.index].reset_index() 
+1

はい、それです! – MaxU

関連する問題