how to convert pair lists (to matrix) to heat map; python

江枫思渺然 提交于 2019-12-12 21:26:48

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/29115909/how-to-convert-pair-lists-to-matrix-to-heat-map-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!