[英]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.