![](/img/trans.png)
[英]How to move and append next row to previous row in the Pandas Dataframe
[英]How to remove next pandas dataframe row when it's equal to previous row only for some columns
我使用以下代码创建了一个名为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)
数据框如下所示:
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
我想删除列值所在的行:
feature1
和feature2
和feature3
与上一行完全相同。 在上面的示例中,我需要删除3
行和4
行,以便生成的数据框如下所示: 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.