将pandas df从长到宽转换为稀疏矩阵

时间:2019-01-15 10:46:42

标签: python pandas sparse-matrix

我有这个数据集:

ARTID    INFO_1         INFO_2 
00001   some_info_11   some_info_21
00002   some_info_12   some_info_22
00003   some_info_13   some_info_23

我想这样转变

ARTID    some_info_11  some_info_12   some_info_13   some_info_21   some_info_22 some_info_23
00001      1                 0           0              1                0             0
00002      0                 1           0              0                1             0

但是我需要成为一个稀疏矩阵。什么是最“内存友好”的方式?

1 个答案:

答案 0 :(得分:1)

使用pd.get_dummies()pd.concat()

df1 = pd.concat([df.ARTID,pd.get_dummies(df[['INFO_1','INFO_2']],prefix='',prefix_sep='')],axis=1)

print(df1)
  ARTID  some_info_11  some_info_12  some_info_13  some_info_21  \
0 00001             1             0             0             1   
1 00002             0             1             0             0   
2 00003             0             0             1             0   

   some_info_22  some_info_23  
0             0             0  
1             1             0  
2             0             1  

如果您允许ARTID作为索引,则可以使用:

pd.get_dummies(df[['INFO_1','INFO_2']],prefix='',prefix_sep='').set_index(df.ARTID)

             some_info_11  some_info_12  some_info_13  some_info_21  some_info_22  \
ARTID                                                                         
00001                 1             0             0             1             0   
00002                 0             1             0             0             1   
00003                 0             0             1             0             0   

          some_info_23  
ARTID                
00001                 0  
00002                 0  
00003                 1