簡體   English   中英

在 Python 中查找兩個數據幀之間的差異

[英]Finding the difference between two dataframes in Python

假設我有兩個數據框

column1 column2 
  abc      2
  def      2

column1 column2 
  abc      2
  def      1

我想比較這兩個數據框並找出差異所在並獲取 column1 的值。

所以在這種情況下輸出應該是'def'

根據這里的答案,您可以嘗試pd.concat方法:

pd.concat([A,B]).drop_duplicates(keep=False)['column1'].unique().tolist()

輸出:

# if you just want to see the differences between the dataframe
>>> pd.concat([A,B]).drop_duplicates(keep=False)
  column1  column2
1     def        2
1     def        1
# if you just want to see the differences and with only 'column1'
>>> pd.concat([A,B]).drop_duplicates(keep=False)['column1']
1    def
1    def
Name: column1, dtype: object
# if you want unique values in the column1 as a numpy array after taking the differences
>>> pd.concat([A,B]).drop_duplicates(keep=False)['column1'].unique()
array(['def'], dtype=object) 
# if you want unique values in the column1 as a list after taking the differences
>>> pd.concat([A,B]).drop_duplicates(keep=False)['column1'].unique().tolist() 
['def']
pd.concat([A,B]).drop_duplicates(keep=False)

暫無
暫無

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

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