[英]Select rows where two or more columns are bigger than 0 in pandas
I am working with a dataframe in pandas. My dataframe had 55 columns and 70.000 rows.我正在使用 pandas 中的 dataframe。我的 dataframe 有 55 列和 70.000 行。
How can I select the rows where two or more values are bigger than 0?我如何 select 两个或多个值大于 0 的行?
It now looks like this:现在看起来像这样:
A B C D E
a 0 2 0 8 0
b 3 0 0 0 0
c 6 2 5 0 0
And would like to make this:并想这样做:
A B C D E F
a 0 2 0 8 0 true
b 3 0 0 0 0 false
c 6 2 5 0 0 true
Have tried converting it to just 0's and 1's and summing that, like so:已尝试将其转换为 0 和 1 并对其求和,如下所示:
df[df > 0] = 1
df[(df > 0).sum(axis=1) >= 2]
But then I lose all the other info in the dataframe and I still want to be able to see the original values.但是后来我丢失了 dataframe 中的所有其他信息,我仍然希望能够看到原始值。
You are close, only assign mask to new column:你很接近,只将掩码分配给新列:
df['F'] = (df > 0).sum(axis=1) >= 2
Or:或者:
df['F'] = np.count_nonzero(df, axis=1) >= 2
print (df)
A B C D E F
a 0 2 0 8 0 True
b 3 0 0 0 0 False
c 6 2 5 0 0 True
Try assigning to a column like this:尝试分配给这样的列:
>>> df['F'] = df.gt(0).sum(axis=1).ge(2)
>>> df
A B C D E F
a 0 2 0 8 0 True
b 3 0 0 0 0 False
c 6 2 5 0 0 True
Or try with astype(bool)
:或者尝试使用astype(bool)
:
>>> df['F'] = df.astype(bool).sum(axis=1).ge(2)
>>> df
A B C D E F
a 0 2 0 8 0 True
b 3 0 0 0 0 False
c 6 2 5 0 0 True
>>>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.