繁体   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