当我尝试使用字符串
过滤Dataframe时出现以下错误 TypeError: Could not compare <type 'str'> type with Series
这是我的代码;
data = pd.read_csv('data.csv')
fildata = data[(data['cat1'] == 'FALSE') & (data['cat2'] != '') & (data['cat3'] == 'FALSE')]
编辑1:
以下是数据的外观;
count,word,cat1,cat2,cat3
1021,.,FALSE,,FALSE
825,the,TRUE,the,FALSE
693,and,TRUE,and,FALSE
647,of,TRUE,of,FALSE
646,",",FALSE,,FALSE
435,to,TRUE,to,FALSE
353,will,TRUE,will,FALSE
297,in,TRUE,in,FALSE
274,be,TRUE,be,FALSE
编辑2:
但为什么会这样呢?
data1 = pd.DataFrame({'cat1':[1,2,3,4],'cat2':[2,3,1,4],'cat3':[3,1,2,4]})
fildata = data1[(data1['cat1'] == 1) & (data1['cat2'] != 0) & (data1['cat3']== 3)]
这导致;
cat1 cat2 cat3
0 1 2 3
编辑3:
我猜问题就是这个类型。 &#39; CAT1&#39; &安培; &#39; CAT2&#39;属于'bool'
答案 0 :(得分:3)
以下对我有用:
In [114]:
temp = """count,word,cat1,cat2,cat3
1021,.,FALSE,,FALSE
825,the,TRUE,the,FALSE
693,and,TRUE,and,FALSE
647,of,TRUE,of,FALSE
646,",",FALSE,,FALSE
435,to,TRUE,to,FALSE
353,will,TRUE,will,FALSE
297,in,TRUE,in,FALSE
274,be,TRUE,be,FALSE"""
data = pd.read_csv(io.StringIO(temp))
fildata = data[(data['cat1'] == False) & (data['cat2'].isnull() ) & (data['cat3'] == False)]
In [115]:
fildata
Out[115]:
count word cat1 cat2 cat3
0 1021 . False NaN False
4 646 , False NaN False
[2 rows x 5 columns]
你遇到的问题是字符串FALSE
/ TRUE
是由read_csv
解释的布尔dtypes:
In [112]:
data.dtypes
Out[112]:
count int64
word object
cat1 bool
cat2 object
cat3 bool
dtype: object
因此您的比较应该针对此类型,而不是字符串'FALSE'