python - 如何将部分数据帧发送到同一数据帧中的相应电子邮件
问题描述
我有一个包含列email
和其他列的数据框。
我需要将此数据发送到相应的电子邮件,例如第 0 行需要通过电子邮件发送到Tony@mail.com
,第 1 行需要通过电子邮件发送到Sergio@mail.com
,第 2 行和第 3 行需要发送到Nico@mail.com
Name Email Subject CreatedDate DueDate FinalDeadLine
0 Tony Tony@mail.com Renewal 2019-12-15 2019-12-16 2019-12-25
1 Sergio Sergio@mail.com NewBusiness 2019-11-18 2019-11-22 2019-11-28
2 Nico Nico@mail.com Endorsement 2019-12-11 2019-12-13 2019-12-24
3 Nico Nico@mail.com Rewrite 2019-12-05 2019-12-07 2019-12-23
使用将数据帧拆分为多个数据帧我正在对数据帧执行视图:
示例代码:
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import pandas as pd
df = pd.DataFrame({
'Name': ['Tony','Sergio','Nico','Nico']
,'Email': ['Tony@mail.com', 'Sergio@mail.com','Nico@mail.com','Nico@mail.com']
,'Subject':['Renewal', 'NewBusiness', 'Endorsement','Rewrite']
,'CreatedDate': ['2019-12-15','2019-11-18','2019-12-11','2019-12-05']
,'DueDate': ['2019-12-16','2019-11-22','2019-12-13','2019-12-07']
,'FinalDeadLine': ['2019-12-25','2019-11-28','2019-12-24','2019-12-23']
})
print(df)
# sort the dataframe
# the parameter axis=1 refer to columns, while 0 refers to rows
df.sort_values(by='Email', axis=0, inplace=True)
# set the index to be this and don't drop
df.set_index(keys=['Email'], drop=False,inplace=True)
# get a list of emails
email_list=df['Email'].unique().tolist()
# now we can perform a lookup on a 'view' of the dataframe
nico = df.loc[df.Email=='Nico@mail.com']
# itrating through email_list and printing dataframe corresponding to each email
for e in email_list:
d = df.loc[df.Email==e]
#print(d)
但是那我怎么能把它和我的send_mail
功能联系起来呢?
发送邮件功能:
user = "myemail@gmail.com"
pwd = "mypassword"
subject = "Test subject"
recipients = "recipients@gmail.com"
def send_email(user,pwd, recipients, subject):
try:
df_html = df.to_html()
dfPart = MIMEText(df_html,'html')
#Container
msg = MIMEMultipart('alternative')
msg['Subject'] = subject
msg['From'] = user
msg['To'] = ",".join(recipients)
msg.attach(dfPart)
server = smtplib.SMTP('smtp.gmail.com: 587')
server.starttls()
server.login(user, pwd)
server.sendmail(user, recipients, msg.as_string())
server.close()
print("Mail sent succesfully!")
except Exception as e:
print(str(e))
print("Failed to send email")
send_email(user,pwd,recipients,"Test Subject")
或者也许有更好、最有效的方法来完成这一切?网上有什么好的例子吗?
解决方案
因此,一旦您完成了对数据框的操作..您可以使用 apply 函数进行逐行操作
轴 = 1
下面是一个例子。
import pandas as pd
df = pd.DataFrame()
df["A"] = [1,2,3,4,5]
df["B"] = ["A", "B", "C", "D", "E"]
# print (df)
def printA(d):
print("row A", d["A"], "row B", d["B"])
df.apply(lambda x:printA(x), axis=1)
row A 1 row B A
row A 2 row B B
row A 3 row B C
row A 4 row B D
row A 5 row B E
来到你的代码。修改电子邮件函数以接受一行作为输入。
def send_email(用户,密码,收件人,主题):
会变成
def send_email(dataFrameRow):
然后在该函数中,您可以使用相同的列名访问每一列..
dataFrameRow["Name"] , dataFrameRow["Subject"], dataFrameRow["Email"]
调用函数就像......你可以在排序之后或 set_index 之后调用它。我不太确定你想在那里实现什么......但这应该适用于你的代码。
df.apply(lambda x: send_email(x), axis=1)
推荐阅读
- entity-framework - 存储在数据库中的 EF 迁移与原始代码不同
- javascript - HTML 标记到 javascript createElement
- mysql - 在laravel中获取一个标题下相同字符串的所有记录
- typescript - 使用过滤的 keyof 类型索引多态“this”类型
- python - 在 Heroku 上安装 numpy 的问题:找不到满足要求 numpy==1.20.1 的版本(来自 -r /tmp/build_795c7f47/requirements.txt
- asp.net - 如何在VB中的if not语句中使用多个条件
- react-native - React Native Expo 实时聊天功能
- java - JSONArrayRequest 没有响应或错误响应
- cmake - 完成 protobuf_generate 后运行命令
- python - Matplotlib fontproperties 仅更改第一个轴刻度