【问题标题】:filter time from attendance log report using csv file使用 csv 文件从考勤日志报告中过滤时间
【发布时间】:2019-08-07 18:01:55
【问题描述】:

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

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

#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 之后的所有出勤情况...

【问题讨论】:

    标签: python pandas csv datetime filter


    【解决方案1】:

    您可以一次在整个数据框上应用这种形式的过滤器:

    filtered_df = original_df[original_df[column_to_filter_on] > somevalue]
    

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

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

    在尝试比较重复时间 (9:30) 与日期时间时会遇到问题,因此,您可以使用 .apply() 引入 late_flag 来比较任何日期时间与 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]
    

    【讨论】:

    • 这是我的 csv 文件
    • 0,29/07/2019 08:22:39,1,sapir 0,29/07/2019 08:31:14,2,gilad 0,29/07/2019 08:37 :33,3,yarin 0,29/07/2019 09:38:02,1,sapir 0,29/07/2019 09:59:22,2,gilad 0,29/07/2019 10:22:39 ,3,yarin 0,29/07/2019 10:31:14,1,sapir 0,29/07/2019 10:37:33,2,gilad 0,29/07/2019 10:38:02,3 ,yarin 0,29/07/2019 10:59:22,1,sapir
    • 文件 "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/indexes/base.py",第 2890 行,在 get_loc 返回self._engine.get_loc(key) 文件“pandas/_libs/index.pyx”,第 107 行,在 pandas._libs.index.IndexEngine.get_loc 文件“pandas/_libs/index.pyx”,第 131 行,在 pandas._libs .index.IndexEngine.get_loc 文件“pandas/_libs/hashtable_class_helper.pxi”,第 1607 行,在 pandas._libs.hashtable.PyObjectHashTable.get_item 文件“pandas/_libs/hashtable_class_helper.pxi”,第 1614 行,在 pandas._libs.hashtable .PyObjectHashTable.get_item KeyError: 1
    • 似乎错误来自df[1] - 请参阅我编辑的答案以获取潜在修复
    • 我按照您的建议进行了修复,但仍然出现错误。
    猜你喜欢
    • 2016-07-20
    • 2012-02-27
    • 2015-10-20
    • 1970-01-01
    • 1970-01-01
    • 2022-11-17
    • 1970-01-01
    • 2019-01-04
    • 2012-05-20
    相关资源
    最近更新 更多