python - 在 Python 中查找两个数据帧之间的差异
问题描述
假设我有两个数据框
答:
column1 column2
abc 2
def 2
乙:
column1 column2
abc 2
def 1
我想比较这两个数据框并找出差异所在并获取 column1 的值。
所以在这种情况下输出应该是'def'
解决方案
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']
推荐阅读
- matlab - 如何找到可以从 MATLAB 中的分区访问的单元分区?
- angular - 具有不同属性名称的 Typescript 映射类型
- c++ - 创建具有特定值的特定格式的 char*
- javascript - jQuery触发onDrop事件反应节点
- lttng - 澄清 lttng 行为和性能:打印到控制台?
- javascript - 将 JSON 从 ajax 返回到 Javascript 中的另一个类
- html - 用于更改 CSS 的兄弟组合器一旦悬停 div 在所有情况下都不起作用
- javascript - Javascript:导入错误并且找不到变量
- qt - 在 Windows 上从 Linux 打开 .ui 文件
- mysql - 仅从 MySQL 查询中返回较高的 ID