亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

獲取年月數的平均值

獲取年月數的平均值

紅糖糍粑 2023-05-23 16:26:19
我試圖找到每個月的平均“工作”數量。即 5 月(2011 年 5 月 + 2012 年 5 月 + 2013 年 5 月...2020 年 5 月)、6 月等創造的平均就業機會是多少?NAME CLOSEDATE  JOBS  month yearA    2019-01-01 2     1     2019B    2019-01-01 23    1     2019C    2018-05-24 2     5     2018D    2019-05-23 200   5     2019E    2020-05-23 40    5     2020F    2020-05-14 23    5     2020G    2020-06-12 93    6     2020我嘗試過: pd.pivot_table(proj, index=['month'],values=['JOBS'],aggfunc=[np.sum,np.mean]) 這給了我當月每條記錄的平均工作量,而不是總月的平均值。在上面的示例數據集中,理想情況下我會得到 5 月份的 66.25 個工作崗位的結果。(2+200+40+23)/4我覺得我缺少一些簡單的東西,或者一種將表格格式化為:Year Jan   Feb   Mar ..... Dec2011 1000  4322  5322      23432012 3423  4322  5322      2343...  1645  4322  5322      23432020 7895  3432  9999      2343AVG. 3491  4099  6491 
查看完整描述

1 回答

?
交互式愛情

TA貢獻1712條經驗 獲得超3個贊

更新


首先獲取每年/每月的總和,然后計算跨年的平均值。


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

這將為您提供按月計算的平均工作總和(跨年份和姓名)。請注意,您可以跳過為年/月創建單獨的列,只有在您想繼續將它們用于其他計算時才將它們放入。


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


查看完整回答
反對 回復 2023-05-23
  • 1 回答
  • 0 關注
  • 136 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號