首页 > 解决方案 > Python:使用 col1('x_y')中的字符串作为坐标创建热图?

问题描述

我想使用位置(x_y)作为坐标,值作为颜色强度来创建一个图。

这是数据框:

Position   Value
1_5      1
1_6     -1
1_7      0.5
2_5      6
2_6     -3
2_7     -5

中间步骤可能是像这样创建一个数据框(x,y 的顺序无关紧要):

   1   2   
5  1   6 
6 -1  -3
7  0.5 -5

标签: pythonpandasheatmap

解决方案


我们这样splitunstack

df.index=pd.MultiIndex.from_tuples(df.Position.str.split('_').apply(tuple))
s=df.Value.unstack(0)
s
     1    2
5  1.0  6.0
6 -1.0 -3.0
7  0.5 -5.0

推荐阅读