pandas.Series.sparse.from_coo#

classmethod Series.sparse.from_coo(A, dense_index=False)[source]#

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

This method takes a scipy.sparse.coo_matrix (coordinate format) as input and returns a pandas Series where the non-zero elements are represented as sparse values. The index of the Series can either include only the coordinates of non-zero elements (default behavior) or the full sorted set of coordinates from the matrix if dense_index is set to True.

Parameters:
Ascipy.sparse.coo_matrix

The sparse matrix in coordinate format from which the sparse Series will be created.

dense_indexbool, default False

If False (default), the index consists of only the coords of the non-null entries of the original coo_matrix. If True, the index consists of the full sorted (row, col) coordinates of the coo_matrix.

Returns:
sSeries

A Series with sparse values.

See also

DataFrame.sparse.from_spmatrix

Create a new DataFrame from a scipy sparse matrix.

scipy.sparse.coo_matrix

A sparse matrix in COOrdinate format.

Examples

>>> from scipy import sparse
>>> A = sparse.coo_matrix(
...     ([3.0, 1.0, 2.0], ([1, 0, 0], [0, 2, 3])), shape=(3, 4)
... )
>>> A
<COOrdinate sparse matrix of dtype 'float64'
    with 3 stored elements and shape (3, 4)>
>>> A.todense()
matrix([[0., 0., 1., 2.],
[3., 0., 0., 0.],
[0., 0., 0., 0.]])
>>> ss = pd.Series.sparse.from_coo(A)
>>> ss
0  2    1.0
   3    2.0
1  0    3.0
dtype: Sparse[float64, nan]