仅对于某些列,如何删除下一个pandas dataframe行等于上一行时

发布于 2025-02-07 02:37:46 字数 955 浏览 3 评论 0原文

我已经使用此代码创建了一个名为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

我想删除列的值:

  • farture1 和< /strong>
  • feature2
  • feature3以前的行完全相同。在上面的示例中,我需要删除行34,以便所得的数据帧看起来像这样:

I have created a dataframe called df with this code:

# 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
  • feature2 and
  • feature3
    are exactly the same as the previous row. In the example above, I need to remove rows 3 and 4, so that the resulting dataframe will look like this:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

思慕 2025-02-14 02:37:46

filter 功能类似列,然后计算上一个和当前行之间的差异,并检查差异是否为0 for All 功能代码>列

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

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文