pandas.Series.sparse.from_coo#

classmethod Series.sparse.from_coo(A, dense_index=False)[源代码]#

使用来自scipy.parse.coo_Matrix的稀疏值创建一个Series。

参数
Ascipy.sparse.coo_matrix
dense_index布尔值,默认为False

如果为FALSE(缺省值),则SparseSeries索引仅由原始coo_Matrix的非空条目的坐标组成。如果为True,则SparseSeries索引由coo_Matrix的完整排序(行、列)坐标组成。

退货
s系列

具有稀疏值的级数。

示例

>>> from scipy import sparse
>>> A = sparse.coo_matrix(
...     ([3.0, 1.0, 2.0], ([1, 0, 0], [0, 2, 3])), shape=(3, 4)
... )
>>> A
<3x4 sparse matrix of type '<class 'numpy.float64'>'
with 3 stored elements in COOrdinate format>
>>> 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]