##書いたコード
from sklearn.datasets import load_iris import pandas as pd import matplotlib.pyplot as plt import seaborn as sns %matplotlib inline iris=load_iris() df=pd.DataFrame(data=iris.data, columns=["sepal length","sepal width","petal length","petal width"]) sns.pairplot(df,hue="species")
###エラー
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) 3079 try: -> 3080 return self._engine.get_loc(casted_key) 3081 except KeyError as err: 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: 'species' The above exception was the direct cause of the following exception: KeyError Traceback (most recent call last) <ipython-input-25-8ad4f0b18072> in <module> ----> 1 sns.pairplot(df,hue="species") ~/opt/anaconda3/lib/python3.8/site-packages/seaborn/_decorators.py in inner_f(*args, **kwargs) 44 ) 45 kwargs.update({k: arg for k, arg in zip(sig.parameters, args)}) ---> 46 return f(**kwargs) 47 return inner_f 48 ~/opt/anaconda3/lib/python3.8/site-packages/seaborn/axisgrid.py in pairplot(data, hue, hue_order, palette, vars, x_vars, y_vars, kind, diag_kind, markers, height, aspect, corner, dropna, plot_kws, diag_kws, grid_kws, size) 1987 # Set up the PairGrid 1988 grid_kws.setdefault("diag_sharey", diag_kind == "hist") -> 1989 grid = PairGrid(data, vars=vars, x_vars=x_vars, y_vars=y_vars, hue=hue, 1990 hue_order=hue_order, palette=palette, corner=corner, 1991 height=height, aspect=aspect, dropna=dropna, **grid_kws) ~/opt/anaconda3/lib/python3.8/site-packages/seaborn/_decorators.py in inner_f(*args, **kwargs) 44 ) 45 kwargs.update({k: arg for k, arg in zip(sig.parameters, args)}) ---> 46 return f(**kwargs) 47 return inner_f 48 ~/opt/anaconda3/lib/python3.8/site-packages/seaborn/axisgrid.py in __init__(self, data, hue, hue_order, palette, hue_kws, vars, x_vars, y_vars, corner, diag_sharey, height, aspect, layout_pad, despine, dropna, size) 1227 # to the axes-level functions, while always handling legend creation. 1228 # See GH2307 -> 1229 hue_names = hue_order = categorical_order(data[hue], hue_order) 1230 if dropna: 1231 # Filter NA from the list of unique hue names ~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/frame.py in __getitem__(self, key) 3022 if self.columns.nlevels > 1: 3023 return self._getitem_multilevel(key) -> 3024 indexer = self.columns.get_loc(key) 3025 if is_integer(indexer): 3026 indexer = [indexer] ~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance) 3080 return self._engine.get_loc(casted_key) 3081 except KeyError as err: -> 3082 raise KeyError(key) from err 3083 3084 if tolerance is not None: KeyError: 'species'
###試したこと
どのサイトでも皆さんはhue="species"と書いて実行してますし、feature_nameで列名を指定するのではなく、実際に直接列名を指定してもみました。
なぜ"species"でエラーが出るのでしょうか?
回答1件
あなたの回答
tips
プレビュー