質問編集履歴

1

該当するコードを更新しました

2023/08/25 03:44

投稿

kento.k0505
kento.k0505

スコア1

test CHANGED
File without changes
test CHANGED
@@ -22,18 +22,6 @@
22
22
  ### 該当のソースコード
23
23
 
24
24
  ```python
25
- dataset = datasets.load(dataset_name='statsbomb', match_id=match_id)
26
- ```
27
-
28
- ### 試したこと
29
-
30
- loadin.pyを見てみたが、StatsBombInputsのクラスがなかった。また、kloppyのgithubを確認してみたが見つけられなかった。
31
-
32
- ### 補足情報(FW/ツールのバージョンなど)
33
-
34
- ここにより詳細な情報を記載してください。
35
- 一応他のコードも書いときます
36
-
37
25
  !pip install statsbomb
38
26
  !pip install kloppy==3.12.0
39
27
 
@@ -49,5 +37,60 @@
49
37
 
50
38
  BASE_URL = 'https://raw.githubusercontent.com/statsbomb/open-data/master/data'
51
39
 
40
+ comps_df = sb.Competitions().get_dataframe()
41
+
42
+ def get_matches_df(competition_id, season_id=None, comps_df=None):
43
+ """試合情報を返すメソッド
44
+ Args:
45
+ - competition_id(int) : 大会id
46
+ - season_id(int, default=None) : シーズンid
47
+ - comps_df(pd.DataFrame, default=None) : 大会情報
48
+ Returns:
49
+ pd.DataFrame : 試合情報
50
+ """
51
+ # if文、season_idがあれば、シーズンを指定してjson形式のデータをdataframe形式に変換する
52
+ # season_idが指定されなければ、大会情報からseason_idをfor loopで回しながらダウンロード、変換する
53
+ if season_id:
54
+ matches_df = pd.DataFrame(requests.get(f'{BASE_URL}/matches/{competition_id}/{season_id}.json').json())
55
+ else:
56
+ # リストの内包表記
57
+ matches_df = pd.concat([pd.DataFrame(requests.get(f'{BASE_URL}/matches/{competition_id}/{season_id}.json').json()) for season_id in comps_df[comps_df.competition_id==competition_id].season_id.tolist()])
58
+
59
+ # ここでのmatches_dfは、エクセルで言うセルの中に辞書形式(dict)で値が入っていて分析しづらいので、それらをカラムに分解する
60
+ c_list = ['competition', 'season', 'home_team', 'away_team', 'stadium', 'competition_stage']
61
+ if competition_id == 53:
62
+ c_list.remove('stadium')
63
+ for c in c_list:
64
+ if c in ['stadium', 'competition_stage']:
65
+ key_list = ['id', 'name']
66
+ c_fixed_list = [f'{c}_{k}' for k in key_list]
67
+
68
+ else:
69
+ key_list = [f'{c}_{k}' for k in ['id', 'name']]
70
+ c_fixed_list = key_list
71
+
72
+ for k, c_fixed in zip(key_list, c_fixed_list):
73
+ matches_df[c_fixed] = matches_df[c].apply(lambda x: x[k] if type(x)==dict else None)
74
+
75
+ # 必要なカラムのみを残して最終形とする
76
+ matches_df = matches_df.drop(c_list+['metadata','referee'], axis=1).sort_values('match_date').reset_index(drop=True)
77
+
78
+ return matches_df
79
+
80
+ competition_id = 53
81
+ season_id = 106
82
+
83
+ get_matches_df(competition_id=competition_id, season_id=season_id, comps_df=comps_df)
84
+
52
85
  match_id = 3835319
86
+ dataset = datasets.load(dataset_name='statsbomb', match_id=match_id)
87
+ ```
53
88
 
89
+ ### 試したこと
90
+
91
+ loadin.pyを見てみたが、StatsBombInputsのクラスがなかった。また、kloppyのgithubを確認してみたが見つけられなかった。
92
+
93
+ ### 補足情報(FW/ツールのバージョンなど)
94
+
95
+ ここにより詳細な情報を記載してください
96
+