Python 如何在熊猫数据框列中选择一系列值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38884466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 21:35:13  来源:igfitidea点击:

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

pythonpython-3.xpandasdataframerange

提问by ShanZhengYang

import pandas as pd
import numpy as np
data = 'filename.csv'
df = pd.DataFrame(data)
df 

        one       two     three  four   five
a  0.469112 -0.282863 -1.509059  bar   True
b  0.932424  1.224234  7.823421  bar  False
c -1.135632  1.212112 -0.173215  bar  False
d  0.232424  2.342112  0.982342  unbar True
e  0.119209 -1.044236 -0.861849  bar   True
f -2.104569 -0.494929  1.071804  bar  False

I would like to select a range for a certain column, let's say column two. I would like to select all values between -0.5 and +0.5. How does one do this?

我想为某个列选择一个范围,比如说 column two。我想选择 -0.5 和 +0.5 之间的所有值。如何做到这一点?

I expected to use

我希望使用

-0.5 < df["two"] < 0.5

But this (naturally) gives a ValueError:

但这(自然地)给出了一个 ValueError:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

I tried

我试过

-0.5 (< df["two"] < 0.5)

But this outputs all True.

但这输出所有True.

The correct output should be

正确的输出应该是

0    True
1    False
2    False
3    False
4    False
5    True

What is the correct way to find a range of values in a pandas dataframe column?

在 Pandas 数据框列中查找一系列值的正确方法是什么?

EDIT: Question

编辑:问题

Using .between()with

使用.between()

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

would would be the difference between

将是之间的区别

 -0.5 < df['two'] < 0.5

and inequalities like

和不平等,如

 -0.5 =< df['two'] < 0.5

?

?

回答by root

Use betweenwith inclusive=Falsefor strict inequalities:

使用betweeninclusive=False严格的不平等:

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

The inclusiveparameter 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:

inclusive参数确定是否包含端点(True: <=, False: <)。这适用于两个标志。如果你想要混合不等式,你需要明确地对它们进行编码:

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

回答by Kartik

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

.between是一个很好的解决方案,但如果你想要更好的控制使用这个:

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

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

运算符&不同于and. 其他运算符是|for or~for not。有关更多信息,请参阅此讨论

Your statement was the same as this:

你的陈述是这样的:

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

Hence it raised the error.

因此它引发了错误。