python - 创建矩阵,并按间隔在每个单元格上添加标签

标签 python python-3.x pandas numpy

我有用于填充观察矩阵的箱和数据:

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_dummiescut ,最后添加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/

相关文章:

python - 使用索引进行散点图图例

python - 重新排列 pandas DataFrame 的列,使总计最高的列排在第一位

python - 我如何改进欧拉问题 7 的代码

python - 如何使用 ElementTree 从深度嵌套的 XML 子元素中提取属性?

python - 如何使用 python pandas 计算给定列的特定行的百分比?

python - 使用 python 进行 Web 抓取 - 不断从 jquery 表中获取重复的第一行值

python - 如何根据元素的某些方面将 Python 列表分成两个列表

没有导数的Python函数最小化

python - 重新findall : Inserting a variable value in regex failing when {} already used for range of occurences to match

python - Image.fromarray 不能与 array.array 一起使用