如何在多索引Pandas数据框中选择大于某个值的像元? [英] How to select cells greater than a value in a multi-index Pandas dataframe?

查看:54
本文介绍了如何在多索引Pandas数据框中选择大于某个值的像元?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试1:

df[ df > 1.0 ]:这返回了NAN中的所有单元格.

df[ df > 1.0 ] : this returned all cells in NAN.

Try2:

df.loc[ df > 1.0 ]:这返回了KeyError: 0

df[df['A']> 1.0]:这可行-但我想将过滤条件应用于所有列.

df[df['A']> 1.0] : this works - But I want to apply the filter condition to all columns.

推荐答案

如果您要尝试仅选择任一列满足条件的行,则可以使用

If what you are trying to do is to select only rows where any one column meets the condition , you can use DataFrame.any() along with axis=1 (to do row-wise grouping) . Example -

In [3]: df
Out[3]:
   A  B  C
0  1  2  3
1  3  4  5
2  3  1  4

In [6]: df[(df <= 2).any(axis=1)]
Out[6]:
   A  B  C
0  1  2  3
2  3  1  4

或者,如果您尝试过滤所有列均满足条件的行,请使用

Alternatively, if you are trying for filtering rows where all columns meet the condition , use .all() inplace of .any() . Example of all -

In [8]: df = pd.DataFrame([[1,2,3],[3,4,5],[3,1,4],[1,2,1]],columns=['A','B','C'])

In [9]: df
Out[9]:
   A  B  C
0  1  2  3
1  3  4  5
2  3  1  4
3  1  2  1

In [11]: df[(df <= 2).all(axis=1)]
Out[11]:
   A  B  C
3  1  2  1

这篇关于如何在多索引Pandas数据框中选择大于某个值的像元?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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