首页 > 解决方案 > 如何过滤数据框中的值,其中列具有字符串列表作为值?

问题描述

        domain          intents  expressionId                     name
0           []               []             8  When I dare to be powerful – to use my strengt...
1           []               []             7  When one door of happiness closes, another ope...
2           []               []             6  The first step toward success is taken when yo...
3           []               []             5                  Get busy living or get busy dying
4  [financial, education]    [resolutions]  4  You know you’re in love when you can’t fall as...
5  [financial, business]     [materialistic]3                         Honesty is the best policy

这是我的数据框,其中包含带有字符串列表的域列。而且,我想要的是只获取具有“域”的行包含“金融”(域==金融),以便我得到以下结果:

        domain          intents  expressionId                     name
4  [financial, education]    [resolutions]  4  You know you’re in love when you can’t fall as...
5  [financial, business]     [materialistic]3                         Honesty is the best policy

到目前为止,我尝试的是使用以下命令:

df['domain'].map(lambda x: 'financial' in x)

这正在返回,objectType 为“bool”的列。

0    False
1    False
2    False
3    False
4     True
5     True
Name: domain, dtype: bool

但我想要的是过滤结果而不是布尔值。

请帮我解决一下这个。谢谢你。

标签: pythonpython-3.xpandasdataframe

解决方案


dfFinaicals = df[df['domain'].map(lambda x: 'financial' in x)] 

这只是询问域并使用它来选择行。你快到了。

dfFinaicals = df[df['domain'].contains('financial')]

更优雅


推荐阅读