w3resource

Pandas Series: sparse.to_coo() function

Series-sparse.to_coo() function

The sparse.to_coo() function is used to create a scipy.sparse.coo_matrix from a SparseSeries with MultiIndex.

Syntax:

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

Parameters:

Name Description Type/Default Value Required / Optional
row_levels Python tuple, Python list tuple/list Required
column_levels Python tuple, Python list tuple/list Required
sort_labels Sort the row and column labels before forming the sparse matrix. bool, default False Required

Returns: y : scipy.sparse.coo_matrix
rows : list (row labels)
columns : list (column labels)

Example:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series([4.0, np.nan, 3.0, 4.0, np.nan, np.nan])
s.index = pd.MultiIndex.from_tuples([(2, 3, 'p', 0),
                                        (2, 3, 'p', 1),
                                        (2, 2, 'q', 0),
                                        (2, 2, 'q', 1),
                                        (3, 2, 'q', 0),
                                        (3, 2, 'q', 1)],
                                        names=['P', 'Q', 'R', 'S'])
ss = s.to_sparse()
P, rows, columns = ss.to_coo(row_levels=['P', 'Q'],
                                 column_levels=['R', 'S'],
                                 sort_labels=True)
P

Output:

<3x4 sparse matrix of type '<class 'numpy.float64'>'
	with 3 stored elements in COOrdinate format>

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series([4.0, np.nan, 3.0, 4.0, np.nan, np.nan])
s.index = pd.MultiIndex.from_tuples([(2, 3, 'p', 0),
                                        (2, 3, 'p', 1),
                                        (2, 2, 'q', 0),
                                        (2, 2, 'q', 1),
                                        (3, 2, 'q', 0),
                                        (3, 2, 'q', 1)],
                                        names=['P', 'Q', 'R', 'S'])
ss = s.to_sparse()
P, rows, columns = ss.to_coo(row_levels=['P', 'Q'],
                                 column_levels=['R', 'S'],
                                 sort_labels=True)
P.todense()

Output:

matrix([[0., 0., 3., 4.],
        [4., 0., 0., 0.],
        [0., 0., 0., 0.]])

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series([4.0, np.nan, 3.0, 4.0, np.nan, np.nan])
s.index = pd.MultiIndex.from_tuples([(2, 3, 'p', 0),
                                        (2, 3, 'p', 1),
                                        (2, 2, 'q', 0),
                                        (2, 2, 'q', 1),
                                        (3, 2, 'q', 0),
                                        (3, 2, 'q', 1)],
                                        names=['P', 'Q', 'R', 'S'])
ss = s.to_sparse()
P, rows, columns = ss.to_coo(row_levels=['P', 'Q'],
                                 column_levels=['R', 'S'],
                                 sort_labels=True)
rows

Output:

[(2, 2), (2, 3), (3, 2)]

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series([4.0, np.nan, 3.0, 4.0, np.nan, np.nan])
s.index = pd.MultiIndex.from_tuples([(2, 3, 'p', 0),
                                        (2, 3, 'p', 1),
                                        (2, 2, 'q', 0),
                                        (2, 2, 'q', 1),
                                        (3, 2, 'q', 0),
                                        (3, 2, 'q', 1)],
                                        names=['P', 'Q', 'R', 'S'])
ss = s.to_sparse()
P, rows, columns = ss.to_coo(row_levels=['P', 'Q'],
                                 column_levels=['R', 'S'],
                                 sort_labels=True)
columns

Output:

[('p', 0), ('p', 1), ('q', 0), ('q', 1)]

Previous: Series-sparse.from_coo() function
Next: Python pandas tutorials



Follow us on Facebook and Twitter for latest update.