質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

3708閲覧

seaborn のpairplotでhueの引数でエラーが出る

hyt

総合スコア2

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2021/11/09 02:58

##書いたコード

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"でエラーが出るのでしょうか?

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

melian

2021/11/09 03:08

df の中に species のカラムが無いからです。。。
hyt

2021/11/09 03:29

本当だ,,,,そう言うことか。。ありがとうございます笑
guest

回答1

0

自己解決

カラムに追加するだけ

投稿2021/11/09 03:30

hyt

総合スコア2

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問