Python 返回dataframe,其值在所有列的特定范围内
如何返回所有列的值都在特定范围内的数据帧。我的数据帧当前的结构如下:Python 返回dataframe,其值在所有列的特定范围内,python,pandas,Python,Pandas,如何返回所有列的值都在特定范围内的数据帧。我的数据帧当前的结构如下: California Texas New York ... 200000 151000 900000 50000 160000 250000 75000 120000 171000 . . . . . .
California Texas New York ...
200000 151000 900000
50000 160000 250000
75000 120000 171000
. . .
. . .
. . .
如何为所有列返回包含150000-200000范围内的值的新数据帧我希望这可能是您的解决方案:
df['California'].between(150000, 200000, inclusive=False)
此处,inclusive
确定是否要包含边True
等于=
而,False
表示
定义条件/标准
>>> gt = df > 100
>>> lt = df < 150
>>> mask = np.logical_and(lt,gt)
如果所有列都必须满足标准,请使用.all(axis=1)
>>lt=df<180
>>>gt=df>30
>>>掩码=np.逻辑和(lt,gt)
>>>面具。全部(1)
0对
1错误
2错误
3错误
4错误
5错误
6错误
7正确
8错误
9错误
数据类型:bool
>>>df[全部屏蔽(1)]
0 1 2 3 4
0 33 131 52 122 40
7 116 131 95 89 102
>>>
是否可能重复您只想要所有列值都符合条件的行?也许您应该包括示例输入和期望的结果:不,我正在寻找一个考虑所有列的解决方案。
>>> gt = df > 100
>>> lt = df < 150
>>> mask = np.logical_and(lt,gt)
>>> mask.any(1)
0 True
1 True
2 False
3 True
4 True
5 True
6 False
7 True
8 False
9 True
dtype: bool
>>> df[mask.any(1)]
0 1 2 3 4
0 33 131 52 122 40
1 235 146 36 4 97
3 192 61 127 220 254
4 124 234 238 215 34
5 86 3 220 105 129
7 116 131 95 89 102
9 21 111 203 55 118
>>> lt = df < 180
>>> gt = df > 30
>>> mask = np.logical_and(lt,gt)
>>> mask.all(1)
0 True
1 False
2 False
3 False
4 False
5 False
6 False
7 True
8 False
9 False
dtype: bool
>>> df[mask.all(1)]
0 1 2 3 4
0 33 131 52 122 40
7 116 131 95 89 102
>>>