Skip to main content
`Ctrl`+`K`

# pandas.DataFrame.sparse.from_spmatrix

classmethod DataFrame.sparse.from_spmatrix(data, index=None, columns=None)

  Create a new DataFrame from a scipy sparse matrix.

  This method converts a scipy sparse matrix into a pandas DataFrame with sparse array columns for memory efficiency.

  Parameters:
    data scipy.sparse.spmatrix

      Must be convertible to csc format.

    index, columns Index, optional

      Row and column labels to use for the resulting DataFrame. Defaults to a RangeIndex.

  Returns:
    DataFrame

      Each column of the DataFrame is stored as a `arrays.SparseArray`.

  See also

  `DataFrame.sparse.to_coo`

    Return the contents of the frame as a sparse SciPy COO matrix.

  Examples

      >>> import scipy.sparse
      >>> mat = scipy.sparse.eye(3, dtype=int)
      >>> pd.DataFrame.sparse.from_spmatrix(mat)
           0    1    2
      0    1    0    0
      1    0    1    0
      2    0    0    1