I am building auto birthday wish project. So far I have managed to loop through the month & date and checked against the date time but I need to get the value of the first column if the date and month matches so the function can forward the email.
with open("birthdays.csv") as birthday_file:
birthday = pd.read_csv(birthday_file)
month_data = birthday.month
date_data = birthday.day
birthday_month = [month for month in month_data]
birthday_date = [day for day in date_data]
The csv file contains following info
name, email, year, month, day
Test, test@email.com,1961,12,21
Charlotte, me@yahoo.com, 2021,08,22
As an example, try something like this:
import pandas as pd
# open the file to a dataframe called 'df'
# df = pd.read_csv('yourFile.csv')
# for this demo use:
df = pd.DataFrame({'name': {0: 'Test', 1: 'Charlotte'},
' email': {0: 'test@email.com', 1: 'me@yahoo.com'},
' year': {0: 1961, 1: 2020},
' month': {0: 12, 1: 8},
' day': {0: 21, 1: 25}})
# remove spaces in the column names
df.columns = df.columns.str.replace(' ', '')
# get the individual columns that make up the date to a column with a datetime format
df = df.astype({'year': 'str', 'month': 'str', 'day': 'str'})
df['date'] = df[['year', 'month', 'day']].agg('-'.join, axis=1)
df['date'] = pd.to_datetime(df['date'], format='%Y-%m-%d')
del df['month'], df['year']
print(df)
'''
>> name email day date
>> 0 Test test@email.com 21 1961-12-21
>> 1 Charlotte me@yahoo.com 25 2020-08-25
'''
# create a function to return all email addresses (as a list)
# where the month matches the current month and the day is one week in the future
def sendReminders(delay=7):
return (df.loc[(df['date'].dt.dayofyear > pd.Timestamp.now().dayofyear) &
(df['date'].dt.dayofyear <= (pd.Timestamp.now().dayofyear + delay)), 'email'].tolist())
# call the function to return a list of emails for reminders but override the 7 days and set to 5 day as an example
print(sendReminders(5))
'''
>> ['me@yahoo.com']
'''
print('\n')
Obviously you keep going and add more functions etc. The point is to clean your data and get the columns correct and in the right format. Once you have the data in a dataframe organized you can do all sorts of calculations. Chances are there is a method already for it and you just need to find it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.