Python:使用 col1 ('x_y') 中的字符串作为坐标创建热图?
Python: Create heatmap using string in col1 ('x_y') as coordinates?
我想创建一个热图,使用位置 (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
我们 split
然后 unstack
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
立即数据帧可以通过以下方式获得:
(df.join(df.Position.str.split('_',expand=True).astype(float))
.set_index([0,1])
.Value.unstack(level=0)
)
输出:
0 1.0 2.0
1
5.0 1.0 6.0
6.0 -1.0 -3.0
7.0 0.5 -5.0
我想创建一个热图,使用位置 (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
我们 split
然后 unstack
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
立即数据帧可以通过以下方式获得:
(df.join(df.Position.str.split('_',expand=True).astype(float))
.set_index([0,1])
.Value.unstack(level=0)
)
输出:
0 1.0 2.0
1
5.0 1.0 6.0
6.0 -1.0 -3.0
7.0 0.5 -5.0