如何在数据框中匹配今天的日期并发送电子邮件 [英] How to match today's date in dataframe and send email messages

查看:98
本文介绍了如何在数据框中匹配今天的日期并发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习了如何使用熊猫来处理数据,我发现它非常酷和有趣。我正在尝试构建一个自动电子邮件系统。



但是现在我有很多库存:
这个问题引用了我的一个老问题



我要检查 datatime_from 是否为今天,如果是真的,请获取该行的电子邮件地址并发送电子邮件。我正在使用pandas作为数据框,我的想法是使用smtplib发送电子邮件,但是我愿意使用其他库。



这是到目前为止的代码:

  import smtplib 
导入熊猫

def send_me_email():
服务器= smtplib.SMTP('smtp.gmail.com',587)
server.starttls()
server.login('xxx@gmail.com','xxxxxxx')

msg =您的消息!
server.sendmail(您的电子邮件地址,发送至的电子邮件地址,msg)
server.quit()

msg =您的消息!
server.sendmail( xxx@gmail.com, xxx@gmail.com,msg)
server.quit()

csv = pandas.read_csv(' testfile.csv',delimiter =',')

#csv ['datetime_from']。dtype

csv ['datetime_from'] = pandas.to_datetime(csv [' datetime_from'],错误=强制)
#csv ['datetime_from']。dtype

today_date = pandas.datetime.today()。date()

csv2 = csv ['datetime_from']。dt.date == today_date

csv ['datetime_from']。where(csv).apply(send_me_email())

这无法正常工作,我无法弄清楚如何在所有实际日期中获取相应的电子邮件地址并发送电子邮件至那些。数据文件将以每天增加的新输入不断增长。



任何想法都会受到赞赏。

解决方案

您将需要使用掩码来索引数据帧:

  csv2 = csv [csv ['datetime_from']。dt.date == today_date] 

此外,您的 send_me_email 必须采用 df.apply 传递给它的参数:

  def send_me_email(电子邮件):
...

此参数是电子邮件。确保您修改了函数的主体以进行相应的处理。



您现在可以使用 df.apply 。不要调用您的函数。您必须通过它(不使用()):

  csv2 [ '电子邮件'] .apply(send_me_email)


I learning how to use pandas to deal with data and I find it pretty cool and fun. I am trying to build an automatic email system.

But now I am stocked with the following: This question makes reference to one of my old questions here.

This is how may data-set looks like:

I want to check if datatime_from is today and if it is true, take the email-address of that row and send an email message. I am using pandas as for the dataframe and my idea was to use smtplib for sending email messages, but I am open to use other libraries.

Here is my code until now:

import smtplib
import pandas 

def send_me_email():
  server = smtplib.SMTP('smtp.gmail.com', 587)
  server.starttls()
  server.login('xxx@gmail.com', 'xxxxxxx')

  msg = "YOUR MESSAGE!"
  server.sendmail("YOUR EMAIL ADDRESS", "THE EMAIL ADDRESS TO SEND TO", msg)
  server.quit()

  msg = "YOUR MESSAGE!"
  server.sendmail("xxx@gmail.com", "xxx@gmail.com", msg)
  server.quit()

csv = pandas.read_csv('testfile.csv', delimiter=',')

#csv['datetime_from'].dtype

csv['datetime_from'] = pandas.to_datetime(csv['datetime_from'], errors='coerce')
#csv['datetime_from'].dtype

today_date = pandas.datetime.today().date()

csv2 = csv['datetime_from'].dt.date == today_date

csv['datetime_from'].where(csv).apply(send_me_email())

This is not working correctly, I cannot figure it out how to grab for all actual dates the corresponding email-addresses and send an email to those. The data file will be growing with new coming input every day.

Any ideas will be appreciated.

解决方案

You'll need to use the mask you get to index your data frame:

csv2 = csv[csv['datetime_from'].dt.date == today_date]

Furthermore, your send_me_email must take a parameter which df.apply passes to it:

def send_me_email(email):
    ...

This parameter is the E-Mail. Make sure you modify the body of your function to handle it accordingly.

You may now apply your function using df.apply. Do not call your function. You must pass it (without the ()):

csv2['E-Mail'].apply(send_me_email)

这篇关于如何在数据框中匹配今天的日期并发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆