Python 3.x 熊猫用具有特殊字符的loc函数查找数据

Python 3.x 熊猫用具有特殊字符的loc函数查找数据,python-3.x,pandas,dataframe,loc,Python 3.x,Pandas,Dataframe,Loc,我试图在数据框中查找不包含此字符串[]的数据 在我的数据集中,我有以下列标记值: 我正在使用下面的代码,但它并不像我期望的那样工作 conversion_rate = deals.loc[(deals["businessCaseType_value"] =='Sale LiteScope') & (deals["tags"] != "[]]") 我在项目的其他地方使用了类似的代码,它工作得很好 hit_rate = len(d

我试图在数据框中查找不包含此字符串[]的数据 在我的数据集中,我有以下列标记值:

我正在使用下面的代码,但它并不像我期望的那样工作

conversion_rate = deals.loc[(deals["businessCaseType_value"] =='Sale LiteScope') & (deals["tags"] != "[]]")
我在项目的其他地方使用了类似的代码,它工作得很好

hit_rate = len(deals.loc[(deals["businessCaseType_value"]=='Sale LiteScope') & (deals["status"]=='E_WIN')].index)
我唯一的问题是,这是由于特殊字符[]

我们将非常感谢您的帮助


谢谢

让我们尝试按列表长度筛选:

将熊猫作为pd导入
df=pd.DataFrame({
“标记”:[[],
['Webinar LiteScope','Something other'],
['Webinar Mountains']]
})
df['TagsLen']=df['Tags'].应用(len)
#具有长度的数据帧
打印(df)
#筛选出空列表
打印(df[df['TagsLen'].ne(0)])
输出:

                                  Tags  TagsLen
0                                   []        0
1  [Webinar LiteScope, Something Else]        2
2                  [Webinar Mountains]        1
                                  Tags
1  [Webinar LiteScope, Something Else]
2                  [Webinar Mountains]
过滤:

                                  Tags  TagsLen
1  [Webinar LiteScope, Something Else]        2
2                  [Webinar Mountains]        1

(在不创建新柱的情况下使用一个衬里)

输出:

                                  Tags  TagsLen
0                                   []        0
1  [Webinar LiteScope, Something Else]        2
2                  [Webinar Mountains]        1
                                  Tags
1  [Webinar LiteScope, Something Else]
2                  [Webinar Mountains]
我补充说

deals.tags =deals.tags.apply(lambda y: np.nan if len(y)==0 else y)
将所有空字符串转换为NaN,然后更新我的代码

conversion_rate = deals.loc[(deals["businessCaseType_value"] =='Sale LiteScope') & deals["tags"].notnull()]

非常感谢

而不是
(deals[“tags”!=“[]”)你的意思是
(deals[“tags”!=“[]”
?另外,您确定“tags”是一列字符串而不是列表吗?您是对的,列标记是列表:-)