1 回答

TA貢獻(xiàn)1712條經(jīng)驗(yàn) 獲得超3個(gè)贊
更新
首先獲取每年/每月的總和,然后計(jì)算跨年的平均值。
by_month = (
proj.groupby([proj.CLOSEDATE.dt.year, proj.CLOSEDATE.dt.month]) # create the groupby object
.JOBS.sum() # select only the JOBS column and aggregate by sum
.unstack(0) # drop the 'year' level form MultiIndex and use as columns
.mean(axis=1) # areage across the years we just unstacked to axis1
.rename('avg_jobs')
.rename_axis('month')
)
print(by_month)
month
1 25.000000
5 88.333333
6 93.000000
Name: avg_jobs, dtype: float64
這將為您提供按月計(jì)算的平均工作總和(跨年份和姓名)。請注意,您可以跳過為年/月創(chuàng)建單獨(dú)的列,只有在您想繼續(xù)將它們用于其他計(jì)算時(shí)才將它們放入。
by_month = (
proj.groupby('month') # create the groupby object
.JOBS.mean() # select only the JOBS column and aggregate by mean
)
print(by_month)
month
1 12.50
5 66.25
6 93.00
Name: JOBS, dtype: float64
添加回答
舉報(bào)