pandas.DataFrame.sparse.to_coo#

DataFrame.sparse.to_coo()[源代码]#

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

返回:
scipy.sparse.spmatrix

如果调用者是异构的并且包含布尔值或对象,则结果将是 dtype=object。请参阅“说明”。

另请参阅

DataFrame.sparse.to_dense

将具有稀疏值的 DataFrame 转换为密集格式。

注意

dtype 将是最低公分母类型(隐式向上转型);也就是说,如果 dtype(即使是数值类型)混合,则会选择能够容纳所有类型的类型。

例如:如果 dtype 是 float16 和 float32,则 dtype 将向上转型为 float32。根据 numpy.find_common_type 的约定,混合 int64 和 uint64 将导致 float64 dtype。

示例

>>> df = pd.DataFrame({"A": pd.arrays.SparseArray([0, 1, 0, 1])})
>>> df.sparse.to_coo()
<COOrdinate sparse matrix of dtype 'int64'
    with 2 stored elements and shape (4, 1)>