How to select a range of values in a pandas dataframe column?

会有一股神秘感。 提交于 2019-11-29 09:23:42

Use between with inclusive=False for strict inequalities:

df['two'].between(-0.5, 0.5, inclusive=False)

The inclusive parameter determines if the endpoints are included or not (True: <=, False: <). This applies to both signs. If you want mixed inequalities, you'll need to code them explicitly:

(df['two'] >= -0.5) & (df['two'] < 0.5)
Kartik

.between is a good solution, but if you want finer control use this:

(0.5 <= df['two']) & (df['two'] < 0.5)

The operator & is different from and. The other operators are | for or, ~ for not. See this discussion for more info.

Your statement was the same as this:

(0.5 <= df['two']) and (df['two'] < 0.5)

Hence it raised the error.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!