python - 按列表过滤一列字符串而不进行完全匹配
问题描述
我有一个如下所示的熊猫数据框:-
Tweets
0 RT @cizzorz: THE CHILLER TRAP *TEMPLE RUN* OBS...
1 Disco Domination receives a change in order to...
2 It's time for the Week 3 #FallSkirmish Trials!...
3 Dance your way to victory in the new Disco Dom...
4 Patch v6.02 is available now with a return fro...
5 Downtime for patch v6.02 has begun. Find out a...
6 ⛏️... soon
7 Launch into patch v6.02 Wednesday, October 10!...
8 Righteous Fury.\n\nThe Wukong and Dark Vanguar...
9 RT @wbgames: WB Games is happy to bring @Fortn...
我也有一个列表,假设如下:-
my_list = ['Launch', 'Dance', 'Issue']
使用以下命令过滤掉数据框:-
ndata = data[data['Tweets'].str.contains( "|".join(my_list), regex=True)].reset_index(drop=True)
如果我有过滤器不工作
Working Not Working
Launch 'launch' , 'launch,' , 'Launch,' ,'LAUNCH','@launch'
预期的输出应该是以下任何单词的句子
'launch' , 'launch,' , 'Launch,' ,'LAUNCH','@launch'
解决方案
您需要确保contains
忽略这种情况:
import re
.
.
.
ndata = data[data['Tweets'].str.contains("|".join(my_list), regex=True,
flags=re.IGNORECASE)].reset_index(drop=True)
# ^^^^^^^^^^^^^^^^^^^
推荐阅读
- windows - 如何避免“让 Windows 尝试修复应用程序使其不模糊”影响我的应用程序?
- android - 我在创建构建时收到 Menifest 合并问题
- diskspace - 容器中的 Apache Pulsar 磁盘使用情况
- excel - 如何在 VBA 中读取保存在数组中的列
- c++ - 链表 - 动态或静态创建节点?
- python - 如果在 django 中将预定的布尔字段设置为 True,我如何使预订失败?
- apache-spark - 使用 Pyspark 处理来自 kafka 流的数据
- python - 在 Python 中使用正则表达式查找字符
- javascript - Vue:单击时从父组件更改组件道具,单选按钮
- vue.js - ReferenceError:窗口未定义 Nuxtjs