简体   繁体   English

在Python中,比较多列的行差异

[英]In Python, compare row diffs for multiple columns

I want to perform a row by row comparison over multiple columns. 我想在多列上执行逐行比较。 I want a single series, indicating if all entries in a row (over several columns) are the same as the previous row. 我想要一个系列,指示一行中的所有条目(多个列)是否与前一行相同。

Lets say I have the following dataframe 假设我有以下数据帧

import pandas as pd
df = pd.DataFrame({'A' : [1, 1, 1, 2, 2], 
                   'B' : [2, 2, 3, 3, 3], 
                   'C' : [1, 1, 1, 2, 2]})

I can compare all the rows, of all the columns 我可以比较所有列的所有行

>>> df.diff().eq(0)
       A      B      C
0  False  False  False
1   True   True   True
2   True  False   True
3  False   True  False
4   True   True   True

This gives a dataframe comparing each series individually. 这给出了一个数据帧,分别比较每个系列。 What I want is the comparison of all columns in one series. 我想要的是比较一个系列中的所有列。

I can achieve this by looping 我可以通过循环来实现这一点

compare_all = df.diff().eq(0)
compare_tot = compare_all[compare_all.columns[0]]
for c in compare_all.columns[1:]:
    compare_tot = compare_tot & compare_all[c]

This gives 这给了

>>> compare_tot
0    False
1     True
2    False
3    False
4     True
dtype: bool

as expected. 正如所料。

Is it possible to achieve this in with a one-liner, that is without the loop? 是否有可能通过单线程实现这一点,即没有环路?

>>> (df == df.shift()).all(axis=1)
0    False
1     True
2    False
3    False
4     True
dtype: bool

You need all 你需要all

In [1306]: df.diff().eq(0).all(1)
Out[1306]:
0    False
1     True
2    False
3    False
4     True
dtype: bool

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

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