AttributeError: 'DataFrame' object has no attribute 'to_sparse'
AttributeError: 'DataFrame' object has no attribute 'to_sparse'
sdf = df.to_sparse()
已弃用。转换为稀疏 DataFrame 的更新方法是什么?
您可以使用scipy创建稀疏矩阵:
scipy.sparse.csr_matrix(df.values)
这些是 pandas 1.0.0+ 中的 updated sparse conversions。
如何将密集转换为稀疏
使用DataFrame.astype()
with the appropriate SparseDtype()
(例如int
):
>>> df = pd.DataFrame({'A': [1, 0, 0, 0, 1, 0]})
>>> df.dtypes
# A int64
# dtype: object
>>> sdf = df.astype(pd.SparseDtype(int, fill_value=0))
>>> sdf.dtypes
# A Sparse[int64, 0]
# dtype: object
或为简洁起见使用字符串别名:
>>> sdf = df.astype('Sparse[int64, 0]')
如何将稀疏转换为密集
使用DataFrame.sparse.to_dense()
:
>>> from scipy import sparse
>>> sdf = pd.DataFrame.sparse.from_spmatrix(sparse.eye(3), columns=list('ABC'))
>>> sdf.dtypes
# A Sparse[float64, 0]
# B Sparse[float64, 0]
# C Sparse[float64, 0]
# dtype: object
>>> df = sdf.sparse.to_dense()
>>> df.dtypes
# A float64
# B float64
# C float64
# dtype: object
如何将稀疏转换为 COO
>>> from scipy import sparse
>>> sdf = pd.DataFrame.sparse.from_spmatrix(sparse.eye(3), columns=list('ABC'))
>>> sdf.dtypes
# A Sparse[float64, 0]
# B Sparse[float64, 0]
# C Sparse[float64, 0]
# dtype: object
>>> df = sdf.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
sdf = df.to_sparse()
已弃用。转换为稀疏 DataFrame 的更新方法是什么?
您可以使用scipy创建稀疏矩阵:
scipy.sparse.csr_matrix(df.values)
这些是 pandas 1.0.0+ 中的 updated sparse conversions。
如何将密集转换为稀疏
使用DataFrame.astype()
with the appropriate SparseDtype()
(例如int
):
>>> df = pd.DataFrame({'A': [1, 0, 0, 0, 1, 0]})
>>> df.dtypes
# A int64
# dtype: object
>>> sdf = df.astype(pd.SparseDtype(int, fill_value=0))
>>> sdf.dtypes
# A Sparse[int64, 0]
# dtype: object
或为简洁起见使用字符串别名:
>>> sdf = df.astype('Sparse[int64, 0]')
如何将稀疏转换为密集
使用DataFrame.sparse.to_dense()
:
>>> from scipy import sparse
>>> sdf = pd.DataFrame.sparse.from_spmatrix(sparse.eye(3), columns=list('ABC'))
>>> sdf.dtypes
# A Sparse[float64, 0]
# B Sparse[float64, 0]
# C Sparse[float64, 0]
# dtype: object
>>> df = sdf.sparse.to_dense()
>>> df.dtypes
# A float64
# B float64
# C float64
# dtype: object
如何将稀疏转换为 COO
>>> from scipy import sparse
>>> sdf = pd.DataFrame.sparse.from_spmatrix(sparse.eye(3), columns=list('ABC'))
>>> sdf.dtypes
# A Sparse[float64, 0]
# B Sparse[float64, 0]
# C Sparse[float64, 0]
# dtype: object
>>> df = sdf.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