- API reference
- Series
- pandas.Serie...
pandas.Series.sparse.to_coo#
- Series.sparse.to_coo(row_levels=(0,),column_levels=(1,),sort_labels=False)[source]#
Create a scipy.sparse.coo_matrix from a Series with MultiIndex.
Use row_levels and column_levels to determine the row and columncoordinates respectively. row_levels and column_levels are the names(labels) or numbers of the levels. {row_levels, column_levels} must bea partition of the MultiIndex level names (or numbers).
- Parameters:
- row_levelstuple/list
- column_levelstuple/list
- sort_labelsbool, default False
Sort the row and column labels before forming the sparse matrix.Whenrow_levels and/orcolumn_levels refer to a single level,set toTrue for a faster execution.
- Returns:
- yscipy.sparse.coo_matrix
- rowslist (row labels)
- columnslist (column labels)
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"],...)>>>sA B C D1 2 a 0 3.0 1 NaN 1 b 0 1.0 1 3.02 1 b 0 NaN 1 NaNdtype: float64
>>>ss=s.astype("Sparse")>>>ssA B C D1 2 a 0 3.0 1 NaN 1 b 0 1.0 1 3.02 1 b 0 NaN 1 NaNdtype: 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)]
On this page