Nic*_*bon -1 python pandas seaborn
我有一个 Pandas DF,我需要创建一个热图。我的数据看起来像这样,我想将年份放在列中,将天放在行中,然后将其与 Seaborn 一起使用以创建热图
我尝试了多种方法,但是当我选择 DF 时,我总是得到“不一致的形状”,所以有什么关于如何转换它的建议吗?
Year 和 Days 是这个系列的索引
2016年
Tuesday 4
Wednesady 6
.....
Run Code Online (Sandbox Code Playgroud)
2017年
Tuesday 4.4
Monday 3.5
....
Run Code Online (Sandbox Code Playgroud)
将 seaborn 导入为 sns ax = sns.heatmap(dayofweek)
如果您有这样的 DataFrame:
years = range(2016,2019)
months = range(1,6)
df = pd.DataFrame(index=pd.MultiIndex.from_product([years,months]))
df['vals'] = np.random.random(size=len(df))
Run Code Online (Sandbox Code Playgroud)
您可以使用以下方法将数据重新格式化为矩形:
df2 = df.reset_index().pivot(columns='level_0',index='level_1',values='vals')
sns.heatmap(df2)
Run Code Online (Sandbox Code Playgroud)