python - 如何过滤数据框中的值,其中列具有字符串列表作为值?
问题描述
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
但我想要的是过滤结果而不是布尔值。
请帮我解决一下这个。谢谢你。
解决方案
dfFinaicals = df[df['domain'].map(lambda x: 'financial' in x)]
这只是询问域并使用它来选择行。你快到了。
dfFinaicals = df[df['domain'].contains('financial')]
更优雅
推荐阅读
- javascript - 可以覆盖验证错误对话框中的文本吗?
- c# - 检测树中的无限循环
- c# - StreamReader(_string) 改为 C:/.../?string
- sql - 为什么 Google 数据分析团队提供的 SQL 子句出现语法错误
- file - Mule 4 SFTP delete 删除但仍然抛出错误文件不存在
- bash - 如何在 Expect 脚本中将 for 循环增加 2?
- c# - c# - 带有模式“#.##”的十进制 toString 没有正确格式化为零 (0) 的值
- php - MySQL数据库的重复条目条件
- python - Django你如何同时在两个列表中循环使用范围
- asp.net - 在 asp:datagrid 中以欧洲格式显示日期