按列表过滤字符串列而不进行完全匹配

时间:2018-10-18 18:50:07

标签: python python-3.x pandas dataframe data-cleaning

我有一个如下所示的熊猫数据框:-

    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'

1 个答案:

答案 0 :(得分:1)

您需要确保contains忽略大小写:

import re

.
.
.

ndata = data[data['Tweets'].str.contains("|".join(my_list), regex=True,                          
                                         flags=re.IGNORECASE)].reset_index(drop=True)
#                                        ^^^^^^^^^^^^^^^^^^^