Convert sparse matrix (csc_matrix) to pandas dataframe

后端 未结 1 1107
情深已故
情深已故 2021-01-02 04:25

I want to convert this matrix into a pandas dataframe. csc_matrix

The first number in the bracket should be the index, the

相关标签:
1条回答
  • 2021-01-02 05:10
    from scipy.sparse import csc_matrix
    
    csc = csc_matrix(np.array(
        [[0, 0, 4, 0, 0, 0],
         [1, 0, 0, 0, 2, 0],
         [2, 0, 0, 1, 0, 0],
         [0, 0, 0, 0, 0, 1],
         [4, 0, 3, 2, 0, 0]]))
    
    # Return a Coordinate (coo) representation of the Compresses-Sparse-Column (csc) matrix.
    coo = csc.tocoo(copy=False)
    
    # Access `row`, `col` and `data` properties of coo matrix.
    >>> pd.DataFrame({'index': coo.row, 'col': coo.col, 'data': coo.data}
                     )[['index', 'col', 'data']].sort_values(['index', 'col']
                     ).reset_index(drop=True)
       index  col  data
    0      0    2     4
    1      1    0     1
    2      1    4     2
    3      2    0     2
    4      2    3     1
    5      3    5     1
    6      4    0     4
    7      4    2     3
    8      4    3     2
    
    0 讨论(0)
提交回复
热议问题