我在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) 我正在尝试对熊猫数据帧进行一些简单的操作.我已将pandas导入pd并将numpy导入为np,并导入csv以创建名为'dfe'的数据框.
我已成功使用以下代码根据一个条件填充新列:
dfe['period'] = np.where(dfe['Time'] >= "07:30:00.000" , '1', '2')
Run Code Online (Sandbox Code Playgroud)
但是当我尝试使用类似的技术根据两个条件填充同一列时,我得到一个错误(&''bool'和'str'的不支持的操作数类型)
这是我对多条件版本的尝试:
dfe['period'] = np.where(dfe['Time'] >= "07:30:00.000" & dfe['Time'] <= "10:00:00.000" , '1', '2')
Run Code Online (Sandbox Code Playgroud)
我已经看过很多针对类似问题的解决方案,但是鉴于我刚刚开始并且希望有人可以给我一些关于为什么这不起作用的线索,它们对我来说有点太复杂了.
谢谢