Populate a Pandas SparseDataFrame from a SciPy Sparse Matrix

前端 未结 3 583
借酒劲吻你
借酒劲吻你 2020-11-27 02:41

I noticed Pandas now has support for Sparse Matrices and Arrays. Currently, I create DataFrame()s like this:

return DataFrame(matrix.toarray(),         


        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-27 03:09

    As of pandas v 0.20.0 you can use the SparseDataFrame constructor.

    An example from the pandas docs:

    import numpy as np
    import pandas as pd
    from scipy.sparse import csr_matrix
    
    arr = np.random.random(size=(1000, 5))
    arr[arr < .9] = 0
    sp_arr = csr_matrix(arr)
    sdf = pd.SparseDataFrame(sp_arr)
    

提交回复
热议问题