我有用于填充观察矩阵的箱和数据:
a = array([0., 14., 29., 43., 58., 72., 86., 101., 115., 130., 144.])
b = array([10, 26, 36, 48, 64, 71, 91, 105, 123, 133, 141])
我期望的结果:
0-13 14-28 29-42 43-57 58-71 72-85 86-100 101-114 115-129 130-144
10 1 0 0 0 0 0 0 0 0 0
26 0 1 0 0 0 0 0 0 0 0
36 0 0 1 0 0 0 0 0 0 0
48 0 0 0 1 0 0 0 0 0 0
64 0 0 0 0 1 0 0 0 0 0
71 0 0 0 0 1 0 0 0 0 0
91 0 0 0 0 0 0 1 0 0 0
最佳答案
使用get_dummies
与 cut
,最后添加set_index
对于 b
数组的索引:
labels = ['{}-{}'.format(i, j - 1) for i, j in zip(a[:-1].astype(int), a[1:].astype(int))]
d = pd.get_dummies((pd.cut(b, a, labels=labels))).set_index(b)
print (d)
0-13 14-28 29-42 43-57 58-71 72-85 86-100 101-114 115-129 \
10 1 0 0 0 0 0 0 0 0
26 0 1 0 0 0 0 0 0 0
36 0 0 1 0 0 0 0 0 0
48 0 0 0 1 0 0 0 0 0
64 0 0 0 0 1 0 0 0 0
71 0 0 0 0 1 0 0 0 0
91 0 0 0 0 0 0 1 0 0
105 0 0 0 0 0 0 0 1 0
123 0 0 0 0 0 0 0 0 1
133 0 0 0 0 0 0 0 0 0
141 0 0 0 0 0 0 0 0 0
130-143
10 0
26 0
36 0
48 0
64 0
71 0
91 0
105 0
123 0
133 1
141 1
如果希望最后一个标签更改为144
,这里是解决方案:
a1 = a[:-1].astype(int)
a2 = a[1:].astype(int)
a2[-1] += 1
labels = ['{}-{}'.format(i, j - 1) for i, j in zip(a1, a2)]
d = pd.get_dummies((pd.cut(b, a, labels=labels))).set_index(b)
print (d)
0-13 14-28 29-42 43-57 58-71 72-85 86-100 101-114 115-129 \
10 1 0 0 0 0 0 0 0 0
26 0 1 0 0 0 0 0 0 0
36 0 0 1 0 0 0 0 0 0
48 0 0 0 1 0 0 0 0 0
64 0 0 0 0 1 0 0 0 0
71 0 0 0 0 1 0 0 0 0
91 0 0 0 0 0 0 1 0 0
105 0 0 0 0 0 0 0 1 0
123 0 0 0 0 0 0 0 0 1
133 0 0 0 0 0 0 0 0 0
141 0 0 0 0 0 0 0 0 0
130-144
10 0
26 0
36 0
48 0
64 0
71 0
91 0
105 0
123 0
133 1
141 1
关于python - 创建矩阵,并按间隔在每个单元格上添加标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50812361/