简体   繁体   English

如何在python中获得持续时间的平均值

[英]how to get mean value of duration in python

I was trying to find the answer, but nothing works.我试图找到答案,但没有任何效果。

This is my data frame:这是我的数据框:

Week_summary_df Week_summary_df

and I need to get mean value of each activity per week.我需要获得每周每项活动的平均值。

This is my code:这是我的代码:

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()

do you have any idea how can I fix it?你知道我该如何解决吗?

Please note that I am a complete beginner and I got a task to create tool which will be gathered information about analysts activities during the day.请注意,我是一个完整的初学者,我的任务是创建工具,该工具将收集有关分析师白天活动的信息。 And this tool needs to first gathered info to csv and then export to excel two kind of information: 1. sum of all activities during the day - I was able to manage 2. average duration of each activity during the week - for example: analyst X during the week was on meeting 30min average而且这个工具需要先收集信息到csv然后导出到excel两种信息:1.白天所有活动的总和-我能够管理2.一周内每个活动的平均持续时间-例如:分析师X 一周内平均 30 分钟开会

Try this:尝试这个:

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)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM