过滤时关于括号的问题:
df.info()
RangeIndex: 4005 entries, 0 to 4004
Data columns (total 41 columns):
currency_str 4005 non-null object
state 4005 non-null object
display(
df[
df['currency_str'] == 'INR'
])
正确显示我的INR行。
当我添加第二个过滤器时,我需要两个括号,否则我会收到错误。
display(
df[
(df['currency_str'] == 'INR') &
(df['state'].str.contains('Done'))
])
与熊猫一起在幕后发生了什么? &是不足够的?这是否与字符串字段特别相关,其中标准包含在''中?
No Brackets
display(
df[
df['currency_str'] == 'INR' &
df['state'].str.contains('Done')
])
TypeError: cannot compare a dtyped [bool] array with a scalar of type [bool]
一组括号
display(
df[
(df['currency_str'] == 'INR' &
df['state'].str.contains('Done'))
])
TypeError: cannot compare a dtyped [bool] array with a scalar of type [bool]
答案 0 :(得分:1)
您应为每个条件添加()
df=pd.DataFrame({'currency_str':['INR','INR','None'],'state':['LOLDone','l','Done']})
df[(df['currency_str'] == 'INR') &(df['state'].str.contains('Done'))]
Out[789]:
currency_str state
0 INR LOLDone