简体   繁体   English

给定使用 python 的最后一列的值,我将如何获得 csv 文件中第一列或第二列的值

[英]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.

相关问题 如何使用python获取CSV文件最后一行的第一个值 - How to get the first value of last row of a CSV file using python 如何通过将最后一列分配为元组的第二个值来读取csv文件? - How can I read in from a csv file by assigning last column as the second value of a tuple? 如果第一列值相同,则Python(CSV)将第二列相加 - Python (CSV) sum second column(s) if first column value is the same 如何从 CSV 文件中的第二列和第三列中获取最大值,而 Python 中没有行 header - How to get max value from second column & min value from third column in CSV file with no row header in Python 如何通过使用Python对CSV文件中的日期进行排序来获取列中的最大值? - How can I get the highest value in a column by sorting the date in a CSV file using Python? 如何使用python(pandas)更新csv文件中所有行的最后一列值 - How to update the last column value in all the rows in csv file using python(pandas) 使用 python 从 csv 文件中获取最后一列的最后一行 - Get last row in last column from a csv file using python 使用python覆盖csv文件中的第一列和最后一列 - Overwrite the first and last column in csv file using python 通过csv文件python中的另一列获取一列的值 - get value of one column by another column in csv file python 如何在Python中将csv文件行列的值转换为(行,列,值) - How can I turn csv file row column value into (row, column, value) in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM