简体   繁体   English

如何在仅某些列等于上一行时删除下一个熊猫数据框行

[英]How to remove next pandas dataframe row when it's equal to previous row only for some columns

I have created a dataframe called df with this code:我使用以下代码创建了一个名为df的数据框:

# initialize list of lists
data = {'ID': [1,2,3,4,5,6,7],
        'feature1': [100,32,100,100,100,93,100],
        'feature2': [100,32,100,100,100,93,100],
        'feature3': [100,32,100,100,100,93,100],
        }
 
# Create DataFrame
df = pd.DataFrame(data)

The dataframe looks like this:数据框如下所示:

print(df)

   ID  feature1  feature2  feature3
0   1       100       100       100
1   2        32        32        32
2   3       100       100       100
3   4       100       100       100
4   5       100       100       100
5   6        93        93        93
6   7       100       100       100

I want to remove the rows in which the values of columns:我想删除列值所在的行:

  • feature1 and feature1
  • feature2 and feature2
  • feature3 are exactly the same as the previous row. feature3一行完全相同。 In the example above, I need to remove rows 3 and 4 , so that the resulting dataframe will look like this:在上面的示例中,我需要删除3行和4行,以便生成的数据框如下所示:

在此处输入图像描述

Filter the feature like columns then calculate difference between previous and current row and check whether the difference is 0 for all the feature columns Filter feature列,然后计算前一行和当前行之间的差异,并检查所有feature列的差异是否为0

df[~df.filter(like='feature').diff().eq(0).all(1)]

   ID  feature1  feature2  feature3
0   1       100       100       100
1   2        32        32        32
2   3       100       100       100
5   6        93        93        93
6   7       100       100       100

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

相关问题 如何在 Pandas Dataframe 中将下一行移动并附加到上一行 - How to move and append next row to previous row in the Pandas Dataframe 如果前一行等于某个值,则从 Pandas df 中删除一行 - Delete a row from a Pandas df if the previous row is equal to some value 将缺失的行值填充为 pandas 数据框中上一行和下一行的平均值 - Fill missing row values as an average of previous and next row in pandas dataframe Pandas Dataframe:如何比较一行的两列中的值是否等于后续行的同一列中的值? - Pandas Dataframe: how can i compare values in two columns of a row are equal to the ones in the same columns of a subsequent row? 合并 pandas dataframe 最后一行中的一些列 - Combine some columns in last row of a pandas dataframe 如何对熊猫中的上一行和下一行值求和 - How to sum previous and next row values in pandas 索引上一行或下一行时可以避免数据帧行循环吗? - Can dataframe row loop be avoided when indexing previous or next row? Append 一行到 pandas dataframe 仅用于 Z99938282F04071859941E18F16EFZ4 列 - Append a row to pandas dataframe for select columns only 如何在 Pandas 的 DataFrame 中获取带有条件的前一行 - How to get previous row with condition in a DataFrame of Pandas Pandas - 使用条件获取一些未指定的上一行/下一行的值 - Pandas - get value of some no specified previous / next row with condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM