pandas.Series.sparse.to_coo#

Series.sparse.to_coo(row_levels=(0,), column_levels=(1,), sort_labels=False)[源代码]#

从具有 MultiIndex 的 Series 创建一个 scipy.sparse.coo_matrix。

使用 row_levels 和 column_levels 分别确定行和列的坐标。row_levels 和 column_levels 是级别名称(标签)或级别编号。{row_levels, column_levels} 必须是 MultiIndex 级别名称(或编号)的一个分区。

参数:
row_levelstuple/list
column_levelstuple/list
sort_labelsbool, 默认 False

在形成稀疏矩阵之前对行和列标签进行排序。当 row_levels 和/或 column_levels 引用单个级别时,设置为 True 以加快执行速度。

返回:
yscipy.sparse.coo_matrix
列表(行标签)
列表(列标签)

例子

>>> s = pd.Series([3.0, np.nan, 1.0, 3.0, np.nan, np.nan])
>>> s.index = pd.MultiIndex.from_tuples(
...     [
...         (1, 2, "a", 0),
...         (1, 2, "a", 1),
...         (1, 1, "b", 0),
...         (1, 1, "b", 1),
...         (2, 1, "b", 0),
...         (2, 1, "b", 1),
...     ],
...     names=["A", "B", "C", "D"],
... )
>>> s
A  B  C  D
1  2  a  0    3.0
         1    NaN
   1  b  0    1.0
         1    3.0
2  1  b  0    NaN
         1    NaN
dtype: float64
>>> ss = s.astype("Sparse")
>>> ss
A  B  C  D
1  2  a  0    3.0
         1    NaN
   1  b  0    1.0
         1    3.0
2  1  b  0    NaN
         1    NaN
dtype: Sparse[float64, nan]
>>> A, rows, columns = ss.sparse.to_coo(
...     row_levels=["A", "B"], column_levels=["C", "D"], sort_labels=True
... )
>>> A
<COOrdinate sparse matrix of dtype 'float64'
    with 3 stored elements and shape (3, 4)>
>>> A.todense()
matrix([[0., 0., 1., 3.],
[3., 0., 0., 0.],
[0., 0., 0., 0.]])
>>> rows
[(1, 1), (1, 2), (2, 1)]
>>> columns
[('a', 0), ('a', 1), ('b', 0), ('b', 1)]