Search code examples
pythonpandasseaborn

Heatmap Alphabetically Sorting Index Values


Need some help figuring out how to fit this...

df = pd.read_csv('data.csv')
df1 = df.pivot(index='Name', columns='Years', values='Apples')
sns.heatmap(df1)

The data.csv looks like this:

Name Years Apples
Henry 2020 2
Bob 2020 2
Johnny 2020 1
Henry 2021 6
Bob 2021 2
Johnny 2021 1

Now when I try to do a heatmap using Seaborn, it prompts out a Value Error stating I can't convert "Henry" from a string to a float.

I figured after googling that I need to pivot the dataframe. Unfortunately, it also alphabetically sorted the "Names" column. Is it possible to maintain the same order as "Henry", "Bob", and "Johnny" instead?

Heatmap Screenshot
Heatmap Screenshot


Solution

  • If you did a print of df1, you will notice that the sorting is already done... the issue is because of pivot, rather than the plotting itself.

    >> print(df1)
    
    Years   2020  2021
    Name              
    Bob        2     2
    Henry      2     6
    Johnny     1     1
    

    So, to NOT sort it by index (Name), you can do this using pandas categorical, which will allow you to define the order to keep it in the same order as in the data... So, the updated code (just the one additional line added)...

    df.Name=pd.Categorical(df.Name,categories=df.Name.unique(),ordered=True)
    df1 = df.pivot(index='Name', columns='Years', values='Apples')
    sns.heatmap(df1)
    

    will give you the required plot as below.

    enter image description here