前提・実現したいこと
groupby()グルーピングした値の平均値を算出し、その値を最下列にインデックス名を”Average”として追加したいです。
イメージは、各Playerの平均値やmax値などを.agg()を用いて算出したものの下に、各項目のチームアベレージを追記する感じです。
ご教授の程よろしくお願い致します。
発生している問題・エラーメッセージ
#データフレームを二つに分けて算出し結合してみたのですが、平均値の方、カラムとインデックスが入れ替わっておりきれいに結合できませんでした。 (Player, count) (Speed, mean) (Speed, 90th) \ (Angle, AA416) NaN NaN NaN (Angle, mean) NaN NaN NaN (Angle, std) NaN NaN NaN (Efficiency, mean) NaN NaN NaN (Player, count) NaN NaN NaN (Speed, 90th) NaN NaN NaN (Speed, amax) NaN NaN NaN (Speed, mean) NaN NaN NaN (Speed, std) NaN NaN NaN Player1 47.0 53.5 87.9 Player2 37.0 50.2 84.7 Player3 42.0 52.5 90.5 Player4 43.0 49.1 90.5 Player5 31.0 57.0 94.6 (Speed, amax) (Speed, std) (Angle, mean) (Angle, std) \ (Angle, AA416) NaN NaN NaN NaN (Angle, mean) NaN NaN NaN NaN (Angle, std) NaN NaN NaN NaN (Efficiency, mean) NaN NaN NaN NaN (Player, count) NaN NaN NaN NaN (Speed, 90th) NaN NaN NaN NaN (Speed, amax) NaN NaN NaN NaN (Speed, mean) NaN NaN NaN NaN (Speed, std) NaN NaN NaN NaN Player1 93.3 26.3 0.0 12.1 Player2 93.4 26.5 -1.3 12.4 Player3 98.1 26.9 -3.0 10.5 Player4 96.9 27.9 -0.4 11.6 Player5 99.8 29.0 1.3 11.3 (Angle, AA416) (Efficiency, mean) 0 (Angle, AA416) NaN NaN 29.0 (Angle, mean) NaN NaN -0.7 (Angle, std) NaN NaN 11.6 (Efficiency, mean) NaN NaN 50.1 (Player, count) NaN NaN 40.0 (Speed, 90th) NaN NaN 89.6 (Speed, amax) NaN NaN 96.3 (Speed, mean) NaN NaN 52.5 (Speed, std) NaN NaN 27.3 Player1 23.4 51.4 NaN Player2 27.0 52.8 NaN Player3 21.4 48.9 NaN Player4 27.9 49.9 NaN Player5 45.2 47.5 NaN
該当のソースコード
python
1import pandas as pd 2import numpy as np 3pd.options.display.float_format="{:.1f}".format 4start,end = "2021/4/1","2021/4/30" 5dates = pd.date_range(start=start,end=end,freq="D") 6players = [f"Player{i}"for i in range(1,6)] 7 8N = 200 9dates = np.random.choice(dates,size=N) 10dates.sort() 11players=np.random.choice(players,size=N) 12 13df = pd.DataFrame({ 14 "Date": dates, 15 "Player": players, 16 "Speed": np.random.sample(N) * 100.0, 17 "Angle": np.random.sample(N) * 40.0 - 20.0, 18 "Efficiency": np.random.sample(N) * 100.0, 19}) 20 21def percentile(n): 22 def percentile_(x): 23 return np.percentile(x, n) 24 percentile_.__name__ = '%sth' % n 25 return percentile_ 26 27def AA416(angle): 28 return 100*angle[(angle>=4.0)&(angle<16.0)].count()/angle.count() 29 30dfx = df.groupby("Player").agg({ 31 "Player":"count", 32 "Speed":[np.mean,percentile(90),np.max,np.std], 33 "Angle":[np.mean,np.std,AA416], 34 "Efficiency":[np.mean], 35}) 36dfy = dfx.mean() 37dfz = pd.concat([dfx,dfy],axis=1) 38print(dfz)
試したこと
データフレームを二つに分けて結合してみました。
補足情報(FW/ツールのバージョンなど)
python3.9.4,vscode
回答1件
あなたの回答
tips
プレビュー