我在Pandas中使用布尔索引.问题是为什么声明:
a[(a['some_column']==some_number) & (a['some_other_column']==some_other_number)]
Run Code Online (Sandbox Code Playgroud)
工作正常,而
a[(a['some_column']==some_number) and (a['some_other_column']==some_other_number)]
Run Code Online (Sandbox Code Playgroud)
存在错误?
例:
a=pd.DataFrame({'x':[1,1],'y':[10,20]})
In: a[(a['x']==1)&(a['y']==10)]
Out: x y
0 1 10
In: a[(a['x']==1) and (a['y']==10)]
Out: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Run Code Online (Sandbox Code Playgroud) 我在python pandas DataFrame中有一个列具有布尔值True/False值,但是为了进一步计算,我需要1/0表示.有快速的熊猫/ numpy方式吗?
编辑:下面的答案似乎没有在numpy的情况下,给定一个具有整数和True/False值的dtype=object数组,返回此类数组.为了在numpy中进行进一步的计算,我必须明确地设置np_values = np.array(df.values, dtype = np.float64).
我试图修改一个DataFrame df只包含列中的值closing_price介于99和101之间的行,并尝试使用下面的代码执行此操作.
但是,我得到了错误
ValueError:Series的真值是不明确的.使用a.empty,a.bool(),a.item(),a.any()或a.all()
我想知道是否有办法在不使用循环的情况下执行此操作.
df = df[(99 <= df['closing_price'] <= 101)]
Run Code Online (Sandbox Code Playgroud)