我试图找到答案,但没有任何效果。
这是我的数据框:
并且我需要获取每周每个活动的平均值。
这是我的代码:
import pandas as pd
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().unstack(level=-1)
today_df["Duration: hh:mm:ss"]=today_df["Duration: hh:mm:ss"]
today_df["Duration: hh:mm:ss"] = pd.to_timedelta(today_df["Duration: hh:mm:ss"])
week_data_df = today_df[["week", "Action", "Duration: hh:mm:ss"]]
print(week_data_df)
week_data_df["Duration: hh:mm:ss"] = pd.to_timedelta(week_data_df["Duration: hh:mm:ss"])
week_summary_df = week_data_df.groupby(["week", "Action"]).mean()
您知道如何解决吗?
尝试一下:
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)))