Key Error with sns.pairplot when using hue

151 Views Asked by At

I am working on a machine learning project, specifically wine quality classification and I am having problem with sns.pairplot. I have run the following

df = pd.read_csv("winequality-red.csv", sep=';')
df.head()
column_labels = data.columns
print(column_labels)

and I get this which is perfect. enter image description here

But when running the code I get this error below: enter image description here

This is what I am trying :

sns.pairplot(df.iloc[:, :7], hue='pH')
plt.show()

If I change the ":7" to include the column "pH" then I get the plots, but should it not work without including?

2

There are 2 best solutions below

3
thetaco On BEST ANSWER

Since you are specifying a hue, that column must be included in the chart. If you remove the hue='pH' specification, then the plot would work without the pH column; similarly, if you set hue to a column that is included, you would have no issues.

The issue is that you are telling sns to place a hue on a column that is not included in the chart.

2
ericclapp On

Try:

sns.pairplot(df, vars = df.columns[0:5], hue ="pH") plt.show()