LoginSignup
1

More than 1 year has passed since last update.

PythonでExcelから年度ごとのデータを抜き出して平均を計算したい件

Last updated at Posted at 2023-03-03

やりたいこと

  • 案件A~Cがあるが、各年度にこなした案件総数の平均値が欲しい
  • たとえば「2020-07-08」に得た総案件数156の平均値31.2が重要
  • 2020-07-09, 2020-07-10, ... と多数あるこれらを年度ごとに集計したい

参考文献

https://ai-inter1.com/pandas-timeseries/#st-toc-h-6
https://an-engineer-note.com/?p=683

ソリューション

正直、泥臭すぎる気がしている…もっといい書き方があるのではないかと思うが分からないし、速度上問題ないのでこれで実行中。

# df_org(DataFrame)に以下のように入っているとする

     案件A    案件B    案件C  date_index
 0      23       0        0  2020-07-08
 1      17       0        0  2020-07-08
 2       0       0       67  2020-07-08
 3      19       0        0  2020-07-08
 4      30       0        0  2020-07-08
 ...
40       0       0       10  2022-04-01
41       0       0        8  2022-06-01
42       0       0        8  2022-06-01
43       0       0        8  2022-06-01
44       0       0        4  2022-10-01
make_report_by_fyear.py
def make_report_by_fyear(df_org):
    # 作業用df_work
    # ずらしのためにindexに指定
    df_work["date_index"] = pd.to_datetime(df_org["date_index"])
    df_work.set_index("date_index", inplace=True)
    # 3ヶ月ずらしで「年度」の数字を作る
    df_work = df_work.shift(-3,freq="M")
    # 案件はいずれか
    df_work['総案件'] = df_work.sum(axis=1)
    # indexを解除
    df_work.reset_index(inplace=True)

    # 案件数の月ごと合計と案件レコード数
    df_monthly_sum = df_work.groupby(pd.Grouper(key="date_index", freq="M")).sum()
    df_monthly_size = df_work.groupby(pd.Grouper(key="date_index", freq="M")).size()
    df_monthly_total = pd.concat([df_monthly_sum, df_monthly_size], axis=1)

    # 案件数の年度ごとの合計
    df_fyear_sum = df_monthly_total.resample('Y').sum()
    df_fyear_sum["平均○○数"] = df_fyear_sum.iloc[:,3] / df_fyear_sum.iloc[:,4]
    # 「xx年度」の文字列を作る
    df_fyear_sum.reset_index(inplace=True)
    df_fyear_sum["年度"] = df_fyear_sum['date_index'].dt.strftime('%Y年度')
    df_fyear_sum.set_index("年度", inplace=True)

    return df_fyear_sum

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1