[英]Calculate the attendance from a CSV file generated using a biometric device
[英]filter time from attendance log report using csv file
我在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之后的所有出勤情況...
您可以將這種形式的過濾器立即應用於整個數據幀:
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]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.