使用csv-fi从考勤日志报告中过滤时间

2024-04-19 12:26:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个csv文件中的员工出勤日志报告,我需要过滤所有迟到的员工出勤(9:30之后)。你知道吗

我创建了一个生成考勤的函数。员工输入身份证并标记出勤情况。程序从计算机时钟获取日期和时间,并将出勤情况存储在日志文件中。你知道吗

#function that generated an attendance 
def attandance_log():

    dnt =  datetime.datetime.now()
    dnt_string = dnt.strftime("%d/%m/%Y %H:%M:%S")
    empid = input("Enter Your ID :")
    empname=input("Enter Your Name :")
    df1 = pd.DataFrame(data=[[dnt_string,empid,empname]],columns=["Today's Date & Time", "Employee's ID", "Employee's Name"])
    with open('/Users/sapir/Documents/python/final project- employee attandance log/attandance_log.csv', 'a') as f:
        df1.to_csv(f, header=False)
    return df1
attandance_df= attandance_log()

#the functions that filters all late attendances:
def late_emp_report():

    df = pd.read_csv('/Users/sapir/Documents/python/final project- employee attandance log/attandance_log.csv',index_col=0)
    #df[1] = pd.to_datetime(df[1], unit='s')
    # Add to employees list existing file
    #df.loc['29/07/2019 09:30:00': ].head()------->???
    #df_filtered = df[(df[1] <= datetime.time(9,30))]------>???

    print (df_filtered)
    with open('/Users/sapir/Documents/python/final project- employee attandance log/emplist.csv', 'w') as f:
        df.to_csv(f, header=False)
    return df


late_emp_report()

我不知道如何创建一个文件,显示所有出席9:30后。。。你知道吗


Tags: 文件csvtologdfdatetime员工users
1条回答
网友
1楼 · 发布于 2024-04-19 12:26:34

您可以一次对整个数据帧应用此窗体中的筛选器:

filtered_df = original_df[original_df[column_to_filter_on] > somevalue]

这将返回一个数据帧,其中包含来自原始数据框的所有行,其中列值column_to_filter_on大于some_value

我不喜欢使用1作为列标题,而是给它起一个名称-以防止以后与索引混淆。你知道吗

当您尝试比较循环时间(9:30)和日期时间时,会遇到一个问题,因此您可以使用late_flag引入.apply()来比较任何日期时间和该日期的9:30。你知道吗

# 'Initialize' datetime column in order to later grab df[1]
df['datetime'] = 0
df['datetime'] = pd.to_datetime(df[1], unit='s')

# Calculate late flag - compare datetime vs 9:30 on the same date for each row
df['late_flag'] = df['datetime'].apply(lambda x: 1 if x > x.replace(hour=9, minute=30, second=0, microsecond=0) else 0)

# Filter out just where late_flag is 1
df_filtered = df[df['late_flag'] == 1]

相关问题 更多 >