【发布时间】:2015-01-22 11:35:50
【问题描述】:
我在 pandas 中有一个 DataFrame,我想根据两列的值从中选择一个行子集。
test_df = DataFrame({'Topic' : ['A','A','A','B','B'], 'Characteristic' : ['Population','Other','Other','Other','Other'], 'Total' : [25, 22, 21, 20, 30]})
当我使用此代码时,它按预期工作并返回第一行:
bool1 = test_df['Topic']=='A'
bool2 = test_df['Characteristic']=='Population'
test_df[bool1 & bool2]
但是当我尝试如下在一行中完成所有操作时,
test_df[test_df['Topic']=='A' & test_df['Characteristic']=='Population']
我得到“TypeError: cannot compare a dtyped [object] array with a scalar of type [bool]”
为什么?有没有一个好方法可以一步完成?
【问题讨论】: