如何将对列表(转换为矩阵)转换为热图; Python [英] how to convert pair lists (to matrix) to heat map; python

查看:76
本文介绍了如何将对列表(转换为矩阵)转换为热图; Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将对列表(转换为矩阵)转换为热图?

How to convert pair lists (to matrix) to heat map?

说,我有一个数据框(熊猫),如下所示...

Say, I have a Dataframe (pandas) like following ...

a   x   0.63
a   y   1.00
a   z   0.22
b   z   0.13
b   x   0.20
b   y   0.58
c   y   0.21
c   z   0.14

我想为这些数据生成热图.

I want to generate heat map for this data.

我需要将其转换为矩阵(&如何?),如下所示...

do I need to convert it into matrix (& how?) like following...

    x       y       z
a   0.63    1.00    0.22
b   0.20    0.58    0.13
c   0.00    0.21    0.14

推荐答案

类似于下面的方法,您的df没有列名,但我分别将它们设置为'a','b','c' :

Something like the following would work, your df didn't have column names but I set them to be 'a','b','c' respectively:

In [20]:

df.pivot(index='a',columns='b').fillna(0)
Out[20]:
      c            
b     x     y     z
a                  
a  0.63  1.00  0.22
b  0.20  0.58  0.13
c  0.00  0.21  0.14

这篇关于如何将对列表(转换为矩阵)转换为热图; Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆