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

后端 未结 6 1412
北荒
北荒 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:51

    Pandas docs talks about an experimental conversion to scipy sparse, SparseSeries.to_coo:

    http://pandas-docs.github.io/pandas-docs-travis/sparse.html#interaction-with-scipy-sparse

    ================

    edit - this is a special function from a multiindex, not a data frame. See the other answers for that. Note the difference in dates.

    ============

    As of 0.20.0, there is a sdf.to_coo() and a multiindex ss.to_coo(). Since a sparse matrix is inherently 2d, it makes sense to require multiindex for the (effectively) 1d dataseries. While the dataframe can represent a table or 2d array.

    When I first responded to this question this sparse dataframe/series feature was experimental (june 2015).

提交回复
热议问题