実現したいこと
pandas.read_htmlを用いてスクレイピングしたデータフレームから特定の行だけを取り出したい
前提
pythonを用いて競馬のデータ整理をしています.
以前はネット競馬(https://race.netkeiba.com/race/result.html?race_id=200701010110)より
import pandas as pd pd.read_html('https://race.netkeiba.com/race/result.html?race_id=200701010110')[0][['単勝オッズ','人気']]
で一番上の表から単勝オッズと人気の行だけを抜き出せていたのですが,最近できなくなりました.
ネットを見てもこの方法しか見つかりませんのでご教授お願いします.
発生している問題・エラーメッセージ
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) Cell In[82], line 1 ----> 1 pd.read_html('https://race.netkeiba.com/race/result.html?race_id=200701010110')[0][['人気','単勝オッズ']] File /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pandas/core/frame.py:3813, in DataFrame.__getitem__(self, key) 3811 if is_iterator(key): 3812 key = list(key) -> 3813 indexer = self.columns._get_indexer_strict(key, "columns")[1] 3815 # take() does not accept boolean indexers 3816 if getattr(indexer, "dtype", None) == bool: File /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pandas/core/indexes/base.py:6070, in Index._get_indexer_strict(self, key, axis_name) 6067 else: 6068 keyarr, indexer, new_indexer = self._reindex_non_unique(keyarr) -> 6070 self._raise_if_missing(keyarr, indexer, axis_name) 6072 keyarr = self.take(indexer) 6073 if isinstance(key, Index): 6074 # GH 42790 - Preserve name from an Index File /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pandas/core/indexes/base.py:6130, in Index._raise_if_missing(self, key, indexer, axis_name) 6128 if use_interval_msg: 6129 key = list(key) -> 6130 raise KeyError(f"None of [{key}] are in the [{axis_name}]") 6132 not_found = list(ensure_index(key)[missing_mask.nonzero()[0]].unique()) 6133 raise KeyError(f"{not_found} not in index") KeyError: "None of [Index(['人気', '単勝オッズ'], dtype='object')] are in the [columns]"
該当のソースコード
pd.read_html('https://race.netkeiba.com/race/result.html?race_id=200701010110')[0][['人気','単勝オッズ']]
回答1件
あなたの回答
tips
プレビュー