本文介绍了带有单列的 Seaborn 热图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,其中包含一些歌词的索引(单词)和一列(计数).我正在尝试根据字数创建热图.

I have a dataframe that has an index (words) and a single column (counts) for some lyrics. I am trying to create a heatmap based on the word counts.

    Cuenta
Que 179
La  145
Y   142
Me  113
No  108

我正在尝试生成这样的热图:

I am trying to produce the heatmap like this:

df1 = pd.DataFrame.from_dict([top50]).T
df1.columns = ['Cuenta']
df1.sort_values(['Cuenta'], ascending = False, inplace=True)

result = df1.pivot(index=df1.index, columns='Cuenta', values=df1.Cuenta.count)
sns.heatmap(result, annot=True, fmt="g", cmap='viridis')
plt.show()

但是,它不断抛出索引"对象没有属性级别"

But, it keeps throwing 'Index' object has no attribute 'levels'

任何想法为什么这不起作用?我尝试将索引或单词用作单独的列,但仍然无效.

Any ideas why this isn't working? I tried using the index or words as a separate column and still doesn't work.

推荐答案

数据是一维的.计数已经存在于数据帧的一列(也是唯一一列)中.没有无意义的方法来旋转这些数据.

The data is one-dimensional. The counts are already present in the one (and only) column of the dataframe. There is no meaningless way to pivot this data.

因此,您可以直接将数据框绘制为热图.

You would hence directly plot the dataframe as a heatmap.

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

df = pd.DataFrame({"Cuenta": [179,145,142,113,108]},
                  index=["Que", "La", "Y", "Me", "No"])

sns.heatmap(df, annot=True, fmt="g", cmap='viridis')

plt.show()

这篇关于带有单列的 Seaborn 热图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 04:12