0

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
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Ram
  • 13
  • 4
  • Welcome to Stack Overflow! Please take the [tour]. Are you talking about the last column or the last two? Please give a complete example including an input value and expected output. You can [edit]. Check out [ask] if you want more tips. – wjandrea Aug 22 '21 at 18:47
  • Beside the point, but you don't need all that stuff in the with-block. Just the first line needs to be in there since it's the only part that uses `birthday_file`. – wjandrea Aug 22 '21 at 18:49
  • Does this answer your question? [how do you filter pandas dataframes by multiple columns](https://stackoverflow.com/questions/22086116/how-do-you-filter-pandas-dataframes-by-multiple-columns) – wjandrea Aug 22 '21 at 18:57
  • BTW, I had to add `skipinitialspace=True` here: `pd.read_csv(birthday_file, skipinitialspace=True)` – wjandrea Aug 22 '21 at 18:58
  • Hi, thanks for the reply. New to this so learning how to use it. If I match the value of last two columns of the same row with datetime function then how would I get the value of first and second column of the same row. Example output Charlotte & me@yahoo.com, matched value 22 and 08 – Ram Aug 22 '21 at 18:59

1 Answers1

0

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.

MDR
  • 2,610
  • 1
  • 8
  • 18