pandas.DataFrame.sparse.from_spmatrix#

classmethod DataFrame.sparse.from_spmatrix(data, index=None, columns=None)[源代码]#

从 scipy 稀疏矩阵创建新的 DataFrame。

参数:
datascipy.sparse.spmatrix

必须可以转换为 csc 格式。

index, columnsIndex, optional

用于生成 DataFrame 的行和列标签。默认为 RangeIndex。

返回:
DataFrame

DataFrame 的每一列都存储为 arrays.SparseArray

另请参阅

DataFrame.sparse.to_coo

将 DataFrame 的内容作为稀疏 SciPy COO 矩阵返回。

示例

>>> 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