在 pandas 数据框中删除全零的行 [英] Drop rows with all zeros in pandas data frame

查看:70
本文介绍了在 pandas 数据框中删除全零的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用pandas dropna()功能删除某些或全部列设置为NA的行.是否有等效的功能来删除所有列的值为0的行?

I can use pandas dropna() functionality to remove rows with some or all columns set as NA's. Is there an equivalent function for dropping rows with all columns having value 0?

P   kt  b   tt  mky depth
1   0   0   0   0   0
2   0   0   0   0   0
3   0   0   0   0   0
4   0   0   0   0   0
5   1.1 3   4.5 2.3 9.0

在此示例中,我们要删除数据帧的前4行.

In this example, we would like to drop the first 4 rows from the data frame.

谢谢!

推荐答案

事实证明,可以将其很好地表达为矢量化形式:

It turns out this can be nicely expressed in a vectorized fashion:

> df = pd.DataFrame({'a':[0,0,1,1], 'b':[0,1,0,1]})
> df = df[(df.T != 0).any()]
> df
   a  b
1  0  1
2  1  0
3  1  1

这篇关于在 pandas 数据框中删除全零的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆