簡體   English   中英

找到合並(外部 - 內部)熊貓DF差異

[英]Finding Merge (Outer - Inner) Pandas DF differences

想要找到兩個外合並和內合並DataFrame之間的區別,而沒有找到任何含NaN行 - 我想用它們保留一些行。 有沒有辦法使用difference方法或最好不必同時創建FrameAFrameB

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

數據B

    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

試圖找到DataFrame的差異......

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

也許你有一個更好的解決方案,這個期望的輸出:

    a   b   d
0   1   4   NaN
1   6   2   NaN

您正在尋找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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM