Search code examples
pythonplotdata-visualizationseabornfacet-grid

Seaborn FacetGrid keyerror


My pandas dataframe looks like this

hours Record Result
04      1     Pass
12      2     Fail
04      3     Good
15      4     Warning

I have 500 rows in my dataframe.I want to plot hours on x axis with the number of records on y axis faceted by result.I need 4 graphs each for pass,fail,good and warning condition.I need to find for each hours how many records fall in each result category.

g = sns.FacetGrid(batch_3, row=batch_3['hours'], col=batch_3['Result'], hue=batch_3['Result'])
g.map(plt.plot, 'Stat')

I am getting the following error

KeyError:'WARNING' 'WARNING' 'GOOD' 'GOOD' 'WARNING' 'WARNING',.....] not in index"

Solution

  • The reason you are getting the KeyError is that you should pass the column name as a string to the FacetGrid parameters, rather than passing the actual column as a series. For example, this would work

    sns.FacetGrid(batch_3, row='hours', hue='Result')
    

    but this would not

    sns.FacetGrid(batch_3, row=batch_3['hours'], hue=batch_3['Result'])