こちらのページはメンター様の方々からお叱りを頂きまして、中断させて戴きます。
***注意
***https://support.yahoo-net.jp/PccFinance/s/article/H000011276
以下のコードでキーエラーが起こってしまうのですが、原因がわからないので、ご指摘お願い致します。
import pandas as pd url = 'https://info.finance.yahoo.co.jp/ranking/' dfs = pd.read_html(url) len(dfs) df = dfs[0] df.head() df['前日比'][0] df['取引値.1'][0] df['取引値'][0] df.columns df.columns = ['順位', 'コード', '市場', '名称', '日付', '取引値', '前日比', '増加値', '出来高', '掲示板'] df.head() df.head() df.tail() df = df.drop(df.index[-1]) df.tail() df.index[-1] df['順位'].dtype df = df.astype({'順位': int}) df['順位'].dtype df['増加値'][0]
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) ~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance) 2645 try: -> 2646 return self._engine.get_loc(key) 2647 except KeyError: pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc() pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc() pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item() pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item() KeyError: '増加値' During handling of the above exception, another exception occurred: KeyError Traceback (most recent call last) <ipython-input-80-4d85add7bb8e> in <module> ----> 1 df['増加値'][0] ~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/frame.py in __getitem__(self, key) 2798 if self.columns.nlevels > 1: 2799 return self._getitem_multilevel(key) -> 2800 indexer = self.columns.get_loc(key) 2801 if is_integer(indexer): 2802 indexer = [indexer] ~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance) 2646 return self._engine.get_loc(key) 2647 except KeyError: -> 2648 return self._engine.get_loc(self._maybe_cast_indexer(key)) 2649 indexer = self.get_indexer([key], method=method, tolerance=tolerance) 2650 if indexer.ndim > 1 or indexer.size > 1: pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc() pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc() pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item() pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item() KeyError: '増加値'
回答1件
あなたの回答
tips
プレビュー