在列表中过滤 pandas 并发送电子邮件
filtering pandas over a list and sending email
我有一个如下所示的 pandas 数据框:-
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']
现在,如果 my_list 中有匹配词,我想过滤行并获取整行并将其作为电子邮件发送或发送到 slack。
就像我应该得到第 no 行的输出是因为它里面有舞蹈词。
3 Dance your way to victory in the new Disco Dom..
我尝试使用下面的代码进行过滤,但每次都给我一个空值
data[data['Tweets'].str.contains('my_list')]
此外,如果我有来自列表中的匹配词,我只想将电子邮件与正文发送到同一行,否则我不想要。
使用regex=True
例如:
data[data['Tweets'].str.contains("|".join(my_list), regex=True)]
这样就搞定了:
import pandas as pd
import numpy as np
from io import StringIO
s = '''
"RT @cizzorz: THE CHILLER TRAP *TEMPLE RUN* OBS..."
"Disco Domination receives a change in order to..."
"It's time for the Week 3 #FallSkirmish Trials!..."
"Dance your way to victory in the new Disco Dom..."
"Patch v6.02 is available now with a return fro..."
"Downtime for patch v6.02 has begun. Find out a..."
"⛏️... soon"
"Launch into patch v6.02 Wednesday, October 10!..."
"Righteous Fury.\n\nThe Wukong and Dark Vanguar..."
"RT @wbgames: WB Games is happy to bring @Fortn... plane 5 [20 , 12, 30]"
'''
ss = StringIO(s)
df = pd.read_csv(ss, sep=r'\s+', names=['Data'])
my_list = ['Launch', 'Dance', 'Issue']
cond = df.Data.str.contains(my_list[0])
for x in my_list[1:]:
cond = cond | df.Data.str.contains(x)
df[cond]
我有一个如下所示的 pandas 数据框:-
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']
现在,如果 my_list 中有匹配词,我想过滤行并获取整行并将其作为电子邮件发送或发送到 slack。
就像我应该得到第 no 行的输出是因为它里面有舞蹈词。
3 Dance your way to victory in the new Disco Dom..
我尝试使用下面的代码进行过滤,但每次都给我一个空值
data[data['Tweets'].str.contains('my_list')]
此外,如果我有来自列表中的匹配词,我只想将电子邮件与正文发送到同一行,否则我不想要。
使用regex=True
例如:
data[data['Tweets'].str.contains("|".join(my_list), regex=True)]
这样就搞定了:
import pandas as pd
import numpy as np
from io import StringIO
s = '''
"RT @cizzorz: THE CHILLER TRAP *TEMPLE RUN* OBS..."
"Disco Domination receives a change in order to..."
"It's time for the Week 3 #FallSkirmish Trials!..."
"Dance your way to victory in the new Disco Dom..."
"Patch v6.02 is available now with a return fro..."
"Downtime for patch v6.02 has begun. Find out a..."
"⛏️... soon"
"Launch into patch v6.02 Wednesday, October 10!..."
"Righteous Fury.\n\nThe Wukong and Dark Vanguar..."
"RT @wbgames: WB Games is happy to bring @Fortn... plane 5 [20 , 12, 30]"
'''
ss = StringIO(s)
df = pd.read_csv(ss, sep=r'\s+', names=['Data'])
my_list = ['Launch', 'Dance', 'Issue']
cond = df.Data.str.contains(my_list[0])
for x in my_list[1:]:
cond = cond | df.Data.str.contains(x)
df[cond]