简体   繁体   English

从两个数据帧中获取差异

[英]Get the differences from two dataframes

I have the following dataframe:我有以下 dataframe:

country国家 coin硬币
USA美国 coin1硬币1
USA美国 coin2硬币2
Mexico墨西哥 coin3硬币3

Each coin is unique, and it can change the country.每枚硬币都是独一无二的,它可以改变国家。 For example:例如:

country国家 coin硬币
USA美国 coin1硬币1
Mexico墨西哥 coin2硬币2
Mexico墨西哥 coin3硬币3

What I'm trying to find is a way to see which lines have changed.我想要找到的是一种查看哪些行已更改的方法。 My desired output:我想要的 output:

country国家 coin硬币
Mexico墨西哥 Coin2硬币2

You could use concat to combine them, and then use drop_duplicates to get the difference.您可以使用concat将它们组合起来,然后使用drop_duplicates来获得差异。 For example:例如:

concat([df1,df2]).drop_duplicates(keep=False)

EDIT:编辑:

To get just the one row, you can get the negation of everything common between the two dataframes by turning applying list to them and using .isin to find commonalities.要只获得一行,您可以通过将list应用到它们并使用.isin来查找共同点来否定两个数据帧之间的所有共同点。

df1[~df1.apply(list,1).isin(df2.apply(list,1))]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM