Pandas sparse dataFrame to sparse matrix, without generating a dense matrix in memory

后端 未结 6 1407
北荒
北荒 2020-11-30 08:48

Is there a way to convert from a pandas.SparseDataFrame to scipy.sparse.csr_matrix, without generating a dense matrix in memory?

sc         


        
6条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-30 09:39

    As of Pandas version 0.25 SparseSeries and SparseDataFrame are deprecated. DataFrames now support Sparse Dtypes for columns with sparse data. Sparse methods are available through sparse accessor, so conversion one-liner now looks like this:

    sparse_matrix = scipy.sparse.csr_matrix(df.sparse.to_coo())
    

提交回复
热议问题