選手スタッツデータの集計
NBAの選手スタッツデータセットから得点集計を行ってみたいと思います。
使用させていただくデータはkaggleから、拝借いたします。
#!/usr/bin/env python # -*- coding: utf-8 -*- import numpy as np import pandas as pd fn = "Player_Totals.csv" df = pd.read_csv(fn) print(df.head())
seas_id season player_id player birth_year pos age experience lg tm g gs ... ft fta ft_percent orb drb trb ast stl blk tov pf pts
0 31136 2024 5025 A.J. Green NaN SG 24.0 2 NBA MIL 56 0.0 ... 17 19 0.895 9.0 55.0 64.0 30 9.0 4.0 12.0 49 252
1 31137 2024 5026 A.J. Lawson NaN SG 23.0 2 NBA DAL 42 0.0 ... 15 23 0.652 14.0 36.0 50.0 20 10.0 3.0 14.0 22 136
2 31138 2024 5027 AJ Griffin NaN SF 20.0 2 NBA ATL 20 0.0 ... 2 2 1.000 2.0 16.0 18.0 5 1.0 2.0 8.0 6 48
3 31139 2024 4219 Aaron Gordon NaN PF 28.0 10 NBA DEN 73 73.0 ... 177 269 0.658 174.0 297.0 471.0 259 56.0 45.0 105.0 142 1013
4 31140 2024 4582 Aaron Holiday NaN PG 27.0 6 NBA HOU 78 1.0 ... 58 63 0.921 23.0 100.0 123.0 140 42.0 6.0 53.0 125 514
NBA選手の通算得点ランキング
print(df['lg'].unique()) # NBAデータだけ抜き出します。 df2 = df[df['lg'] == "NBA"] df2 = df2[df2['tm'] != "TOT"]
# 必要な列名をリネーム df2 = df2.rename(columns={'player': 'Player', 'g': 'G', 'fg': 'FGM', 'x3p': '3PM', 'ft': 'FTM'}) # 必要な列だけにする df3 = df2[['player_id', 'Player', 'G', 'FGM', '3PM', 'FTM']] print(df3.head())
選手でグループ化
# 選手でグループ化 players = df3.groupby(["player_id", "Player"]) # グループ毎の合計を出す score_sum = players.sum() # 総合得点順にソート score_sum = score_sum.sort_values('PTS', ascending=False) # 1試合あたりの平均得点の列を作る score_sum['PPG'] = np.round(score_sum['PTS'] / score_sum['G'], 2) # インデックス解除 score_sum.reset_index(inplace=True) # player_id 削除 score_sum.drop("player_id", axis=1, inplace=True) print(score_sum)
Player | PTS | G | FGM | 3PM | FTM | PPG | |
---|---|---|---|---|---|---|---|
0 | LeBron James | 40474 | 1492 | 14837 | 2410 | 8390 | 27.13 |
1 | Kareem Abdul-Jabbar | 38387 | 1560 | 15837 | 1 | 6712 | 24.61 |
2 | Karl Malone | 36928 | 1476 | 13528 | 85 | 9787 | 25.02 |
3 | Kobe Bryant | 33643 | 1346 | 11719 | 1827 | 8378 | 24.99 |
4 | Michael Jordan | 32292 | 1072 | 12192 | 581 | 7327 | 30.12 |
5 | Dirk Nowitzki | 31560 | 1522 | 11169 | 1982 | 7240 | 20.74 |
6 | Wilt Chamberlain | 31419 | 1045 | 12681 | 0 | 6057 | 30.07 |
7 | Kevin Durant | 28924 | 1061 | 9950 | 2031 | 6993 | 27.26 |
8 | Shaquille O'Neal | 28596 | 1207 | 11330 | 1 | 5935 | 23.69 |
9 | Carmelo Anthony | 28289 | 1260 | 10119 | 1731 | 6320 | 22.45 |
10 | Moses Malone | 27409 | 1329 | 9435 | 8 | 8531 | 20.62 |
11 | Elvin Hayes | 27313 | 1303 | 10976 | 5 | 5356 | 20.96 |
12 | Hakeem Olajuwon | 26946 | 1238 | 10749 | 25 | 5423 | 21.77 |
13 | Oscar Robertson | 26710 | 1040 | 9508 | 0 | 7694 | 25.68 |
14 | Dominique Wilkins | 26668 | 1074 | 9963 | 711 | 6031 | 24.83 |
15 | Tim Duncan | 26496 | 1392 | 10285 | 30 | 5896 | 19.03 |
16 | Paul Pierce | 26397 | 1343 | 8668 | 2143 | 6918 | 19.66 |
17 | John Havlicek | 26395 | 1270 | 10513 | 0 | 5369 | 20.78 |
18 | Kevin Garnett | 26071 | 1462 | 10505 | 174 | 4887 | 17.83 |
19 | James Harden | 25885 | 1072 | 7643 | 2940 | 7659 | 24.15 |
20 | Vince Carter | 25728 | 1541 | 9293 | 2290 | 4852 | 16.70 |
21 | Alex English | 25613 | 1193 | 10659 | 18 | 4277 | 21.47 |
22 | Reggie Miller | 25279 | 1389 | 8241 | 2560 | 6237 | 18.20 |
23 | Russell Westbrook | 25211 | 1162 | 9055 | 1273 | 5828 | 21.70 |
24 | Jerry West | 25192 | 932 | 9016 | 0 | 7160 | 27.03 |
25 | Patrick Ewing | 24815 | 1183 | 9702 | 19 | 5392 | 20.98 |
26 | Ray Allen | 24505 | 1300 | 8567 | 2973 | 4398 | 18.85 |
27 | Allen Iverson | 24368 | 914 | 8467 | 1059 | 6375 | 26.66 |
28 | Charles Barkley | 23757 | 1073 | 8435 | 538 | 6349 | 22.14 |
29 | Stephen Curry | 23668 | 956 | 8084 | 3747 | 3753 | 24.76 |
0 件のコメント:
コメントを投稿