sdf = df.to_sparse()
has been deprecated. What's the updated version of this?
CodePudding user response:
These are the updated sparse conversions as of pandas 1.0.0.
To convert dense to sparse
Use DataFrame.astype()
with the appropriate SparseDtype()
(e.g., int
):
>>> df = pd.DataFrame({'A': [1, 0, 0, 1]})
>>> df.dtypes
# A int64
# dtype: object
>>> df = df.astype(pd.SparseDtype(int, fill_value=0))
>>> df.dtypes
# A Sparse[int64, 0]
# dtype: object
Or use the string alias for brevity:
>>> df = df.astype('Sparse[int64, 0]')
To convert sparse to dense
Use DataFrame.sparse.to_dense()
:
>>> from scipy import sparse
>>> df = pd.DataFrame.sparse.from_spmatrix(sparse.eye(3), columns=list('ABC'))
>>> df.dtypes
# A Sparse[float64, 0]
# B Sparse[float64, 0]
# C Sparse[float64, 0]
# dtype: object
>>> df = df.sparse.to_dense()
>>> df.dtypes
# A float64
# B float64
# C float64
# dtype: object
To convert sparse to COO
Use DataFrame.sparse.to_coo()
:
>>> from scipy import sparse
>>> df = pd.DataFrame.sparse.from_spmatrix(sparse.eye(3), columns=list('ABC'))
>>> df.dtypes
# A Sparse[float64, 0]
# B Sparse[float64, 0]
# C Sparse[float64, 0]
# dtype: object
>>> df = df.sparse.to_coo()
# <3x3 sparse matrix of type '<class 'numpy.float64'>'
# with 3 stored elements in COOrdinate format>
# (0, 0) 1.0
# (1, 1) 1.0
# (2, 2) 1.0
CodePudding user response:
You can use scipy to create sparse matrix:
scipy.sparse.csr_matrix(df.values)