简体   繁体   English

并排检查两个pandas数据帧的列之间的差异

[英]Check for differences between the columns of two pandas data frames side by side

I thought this solution would solve my problem but the op here needed to check if the rows of his two data frames contained a difference. 我认为这个解决方案可以解决我的问题,但是这里需要检查他的两个数据帧的行是否包含差异。 I want to do the same but for the columns. 我想对列进行相同的操作。 The solution was ne = (df1 != df2).any(1) but that does not help with my columns. 解决方案是ne = (df1 != df2).any(1)但这对我的列没有帮助。 Yes, I just checked and both of my dataframes have exactly the same shape . 是的,我刚检查过,我的两个数据帧都具有完全相同的shape If I do df1 == df2 it gives me a new data frame full of trues and falses. 如果我做df1 == df2它会给我一个充满真实和虚假的新数据框。 Looking at the first hundred rows it looks like most of the columns with a few exceptions are equal. 查看前100行,看起来大多数列都有相同的例外情况。 How can you just get one True / False for each column? 你怎么能为每一列得到一个真/假?

Here is a toy example: 这是一个玩具示例:

import numpy as np
import pandas as pd
df1 = pd.DataFrame(np.random.randint(low=0, high=10, size=(5, 5)),  columns=['a', 'b', 'c', 'd', 'e'])
df2 = df1.copy()
df2.at[3,'d'] += 10

DF1

DF2

Desired output: 期望的输出:

A True
B True
C True
D False
E True

Use DataFrame.all for check if all values per rows are True s: 使用DataFrame.all检查每行的所有值是否为True

print ((df1 == df2).all())
a     True
b     True
c     True
d    False
e     True
dtype: bool

Detail: 详情:

print (df1 == df2)

      a     b     c      d     e
0  True  True  True   True  True
1  True  True  True   True  True
2  True  True  True   True  True
3  True  True  True  False  True
4  True  True  True   True  True

Solution with any is possible also, only need invert output by ~ : any解决方案也是可能的,只需要反转输出~

print (~((df1 != df2).any()))

a     True
b     True
c     True
d    False
e     True
dtype: bool

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

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