简体   繁体   English

pandas:如何根据X列数是否大于数字来选择行?

[英]pandas: How do I select rows based on if X number of columns is greater than a number?

我可以使用data[data[data > 10].any(1)]来选择任何列大于10的行。如果我想选择任何5列大于10的行,该怎么办?

The following should work for you: 以下内容对您有用:

data[data[data > 10].count(axis=1) > 5]

example: 例:

In [66]:

df = pd.DataFrame({'a':randn(10), 'b':randn(10), 'c':randn(10), 'd':randn(10), 'e':randn(10), 'f':randn(10), 'g':randn(10)})
df
Out[66]:
          a         b         c         d         e         f         g
0 -2.617089 -0.882830  0.678067 -0.517271  0.451493  1.233842  2.039522
1 -0.099578  0.316943  0.707360  0.408488 -0.127735  0.587747 -0.472138
2 -0.717133  0.804504  1.290014  0.091469 -0.311926  0.493378  0.337818
3  0.443820  1.148113  0.437646  0.098209 -0.072878  1.741442  1.954516
4  1.674700 -1.155307 -0.464377 -1.403315 -2.009475 -0.358216  0.430474
5 -0.243898  0.457013 -1.303361 -0.259384 -0.618927 -0.115834  0.062917
6 -1.731918  0.582375  0.007224 -0.336893 -0.092084  0.724403  0.622719
7  0.377062 -0.475285 -1.343725  0.572877  0.613228  0.573816  0.854494
8  0.063625 -0.484536  0.093442 -1.015500  1.062488 -1.818364 -1.139001
9 -0.349160  0.731415  0.418029 -0.341685 -0.421163  0.105534  0.642873

[10 rows x 7 columns]
In [85]:

df[df > 1.0]
Out[85]:
        a         b         c   d         e         f         g
0     NaN       NaN       NaN NaN       NaN  1.233842  2.039522
1     NaN       NaN       NaN NaN       NaN       NaN       NaN
2     NaN       NaN  1.290014 NaN       NaN       NaN       NaN
3     NaN  1.148113       NaN NaN       NaN  1.741442  1.954516
4  1.6747       NaN       NaN NaN       NaN       NaN       NaN
5     NaN       NaN       NaN NaN       NaN       NaN       NaN
6     NaN       NaN       NaN NaN       NaN       NaN       NaN
7     NaN       NaN       NaN NaN       NaN       NaN       NaN
8     NaN       NaN       NaN NaN  1.062488       NaN       NaN
9     NaN       NaN       NaN NaN       NaN       NaN       NaN

[10 rows x 7 columns]
In [86]:

df[df[df > 1.0].count(axis=1) > 2]

Out[86]:
         a         b         c         d         e         f         g
3  0.44382  1.148113  0.437646  0.098209 -0.072878  1.741442  1.954516

[1 rows x 7 columns]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在熊猫中选择和存储大于数字的列? - How do I select and store columns greater than a number in pandas? 如何删除 Pandas 中包含少于 1% 行数的非零的列? - How do I remove columns in Pandas that contains non-zero in less than 1% of number of rows? 如何在Pandas中选择具有超过一定数量的丢失数据的行/列? - How do I select row / columns with more than certain number of missing data in Pandas? 如何只打印列号精确大于 x 的行? 表 Api 和 Python - How can I print only rows that have a precise column number greater than x ? Sheets Api with Python 如何使用Pandas根据列值选择行? - How do I select rows based on columns values with Pandas? 如何从pandas数据框中选择平均值大于某些限制的列? - How do I select columns from pandas dataframe that have an average value greater than some limit? 如何创建包含行大于数字的列的DataFrame? - How do I create a DataFrame containing a column where rows are greater than a number? "如何检索平均值大于某个数字的二维整数 NumPy 数组的列?" - How do I retrieve the columns of a 2D integer NumPy array that have a mean greater than a certain number? 如何删除索引大于 x 的列? - How do I drop columns with index greater than x? 如何随机选择每组固定数量的行(如果更大),否则选择熊猫中的所有行? - How to randomly select fixed number of rows (if greater) per group else select all rows in pandas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM