我试图找到答案,但是没有任何效果。
这是我的数据框:
我需要获取每周每项活动的平均值。
这是我的代码:
from tkinter import *
from datetime import datetime, date
import csv
import pandas as pd
main_window = Tk()
main_window.title('')
def button_bau():
current_date = date.today()
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
comment = entry_comment.get()
data = {'Date': current_date, 'Time': current_time, 'Action': 'BAU', 'Comment': comment}
with open('timesheet.csv', 'a') as csvFile:
writer = csv.DictWriter(csvFile, data.keys())
if csvFile.tell() == 0:
writer.writeheader()
writer.writerow(data)
print(data)
entry_comment.delete(0, END)
def button_training():
current_date = date.today()
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
comment = entry_comment.get()
data = {'Date': current_date, 'Time': current_time, 'Action': 'Training', 'Comment': comment}
with open('timesheet.csv', 'a') as csvFile:
writer = csv.DictWriter(csvFile, data.keys())
if csvFile.tell() == 0:
writer.writeheader()
writer.writerow(data)
print(data)
entry_comment.delete(0, END)
def button_meeting():
current_date = date.today()
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
comment = entry_comment.get()
data = {'Date': current_date, 'Time': current_time, 'Action': 'Meeting', 'Comment': comment}
with open('timesheet.csv', 'a') as csvFile:
writer = csv.DictWriter(csvFile, data.keys())
if csvFile.tell() == 0:
writer.writeheader()
writer.writerow(data)
print(data)
entry_comment.delete(0, END)
def button_break():
current_date = date.today()
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
comment = entry_comment.get()
data = {'Date': current_date, 'Time': current_time, 'Action': 'Break', 'Comment': comment}
with open('timesheet.csv', 'a') as csvFile:
writer = csv.DictWriter(csvFile, data.keys())
if csvFile.tell() == 0:
writer.writeheader()
writer.writerow(data)
print(data)
entry_comment.delete(0, END)
def button_comment():
current_date = date.today()
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
comment = entry_comment.get()
data = {'Date': current_date, 'Time': current_time, 'Action': 'EOD', 'Comment': comment}
with open('timesheet.csv', 'a') as csvFile:
writer = csv.DictWriter(csvFile, data.keys())
if csvFile.tell() == 0:
writer.writeheader()
writer.writerow(data)
print(data)
entry_comment.delete(0, END)
def myFormatString(bigString):
return bigString.replace("0 days ", "").replace(".000000000", "")
def export_to_excel():
today_df = pd.read_csv('timesheet.csv')
today_df["full start date"] = pd.to_datetime(today_df["Date"] + " " + today_df["Time"])
end_times_list = today_df["Time"].to_list()[1:]
today_df["end time"] = pd.Series(end_times_list)
end_dates_list = today_df["Date"].to_list()[1:]
today_df["end date"] = pd.Series(end_dates_list)
today_df["full end date"] = pd.to_datetime(today_df["end date"] + " " + today_df["end time"])
today_df["Duration: hh:mm:ss"] = pd.to_timedelta(today_df["full end date"] - today_df["full start date"])
today_df["week"] = today_df["full start date"].dt.week
day_data_df = today_df[["Date", "Action", "Duration: hh:mm:ss"]]
day_summary_df = day_data_df.groupby(["Date", "Action"]).sum().astype(str).applymap(myFormatString).unstack(level=-1)
today_df["Duration: hh:mm:ss"]=today_df["Duration: hh:mm:ss"]
week_data_df = today_df[["week", "Action", "Duration: hh:mm:ss"]]
week_summary_df = week_data_df.groupby(["week", "Action"]).sum().astype(str).applymap(myFormatString).unstack(level=-1)
print(week_summary_df)
excel_filename = 'timesheet.xlsx'
writer = pd.ExcelWriter(excel_filename, engine='xlsxwriter',
datetime_format='hh:mm:ss')
day_summary_df.drop(day_summary_df.columns[2], axis=1, inplace=True)
day_summary_df.to_excel(excel_filename)
day_summary_df.to_excel(writer, sheet_name='Daily Summary')
week_summary_df.drop(week_summary_df.columns[2], axis=1, inplace=True)
week_summary_df.to_excel(excel_filename)
week_summary_df.to_excel(writer, sheet_name='Weekly Summary')
writer.save()
l1 = Label(main_window, text='Timesheet Collector')
l1.grid(row=0, column=1, pady=5)
b1 = Button(main_window, text='BAU', width=8, bg='#CC8899', command=button_bau)
b1.grid(row=1, column=1, pady= 5, padx=60)
b2 = Button(main_window, text='Training', width=8, bg='#CC8899', command=button_training)
b2.grid(row=2, column=1, pady=5, padx=60)
b3 = Button(main_window, text="Meeting", width=8, bg='#CC8899', command=button_meeting)
b3.grid(row=3, column=1, pady=5, padx=60)
b4 = Button(main_window, text="Break", width=8, bg='#CC8899', command=button_break)
b4.grid(row=4, column=1, pady=5, padx=60)
b5 = Button(main_window, text="EOD", width=8, bg='#E0115F', command=button_comment)
b5.grid(row=5, column=1, pady=5, padx=60)
entry_comment = Entry(main_window, width=25)
entry_comment.grid(row= 6, column=1, pady=10)
b6 = Button(main_window, text="Export to Excel", width=11, bg='#C0C0C0', command=export_to_excel)
b6.grid(row=7, column=1, pady=5, padx=60)
main_window.mainloop()
您是否知道如何解决?
请注意,我是一个完整的初学者,我接到了创建工具的任务,该工具将收集有关白天分析师活动的信息。并且此工具需要首先将信息收集到csv,然后导出为excel两种信息: 1.当天所有活动的总和-我能够管理 2.一周中每个活动的平均持续时间-例如:分析师X在一周中达到30分钟的平均值
答案 0 :(得分:0)
尝试一下:
for col in today_df :
print(df[col].apply(lambda x: x.seconds).groupby([pd.Grouper(freq='W-MON')]).mean().apply(lambda x: timedelta(seconds=x)))