Skip to main content
`Ctrl`+`K`

# pandas.Series.sparse.to_coo

Series.sparse.to_coo(row_levels=(0,), column_levels=(1,), sort_labels=False)

  Create a scipy.sparse.coo_matrix from a Series with MultiIndex.

  Use row_levels and column_levels to determine the row and column coordinates respectively. row_levels and column_levels are the names (labels) or numbers of the levels. {row_levels, column_levels} must be a partition of the MultiIndex level names (or numbers).

  Parameters:
    row_levels tuple/list

      MultiIndex levels to use for row coordinates, specified by name or index.

    column_levels tuple/list

      MultiIndex levels to use for column coordinates, specified by name or index.

    sort_labels bool, default False

      Sort the row and column labels before forming the sparse matrix. When row_levels and/or column_levels refer to a single level, set to True for a faster execution.

  Returns:
    y scipy.sparse.coo_matrix

      The sparse matrix in coordinate format.

    rows list (row labels)

      Labels corresponding to the row coordinates.

    columns list (column labels)

      Labels corresponding to the column coordinates.

  See also

  `Series.sparse.from_coo`

    Create a Series with sparse values from a scipy.sparse.coo_matrix.

  Examples

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