[英]How would I get the value of first or second column in csv file given the value of last column using python
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.
到目前为止,我已经设法遍历月份和日期并检查日期时间,但如果日期和月份匹配,我需要获取第一列的值,以便 function 可以转发 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 csv 文件包含以下信息
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.
整理好 dataframe 中的数据后,您就可以进行各种计算了。 Chances are there is a method already for it and you just need to find it.
很可能已经有一种方法,你只需要找到它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.