- 用法:- Series.sparse.to_coo(row_levels=(0,), column_levels=(1,), sort_labels=False)
- 从具有 MultiIndex 的系列创建 scipy.sparse.coo_matrix。 - 使用row_levels和column_levels分别确定行坐标和列坐标。 row_levels 和column_levels 是级别的名称(标签)或编号。 {row_levels, column_levels} 必须是 MultiIndex 级别名称(或数字)的分区。 - row_levels:元组/列表
- column_levels:元组/列表
- sort_labels:布尔值,默认为 False
- 在形成稀疏矩阵之前对行和列标签进行排序。当 - row_levels和/或- column_levels引用单个级别时,设置为- True以加快执行速度。
 
- y:scipy.sparse.coo_matrix
- rows:列表(行标签)
- columns:列表(列标签)
 
 - 参数:- 返回:- 例子:- >>> 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 <3x4 sparse matrix of type '<class 'numpy.float64'>' with 3 stored elements in COOrdinate format> >>> 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)]
相关用法
- Python pandas.Series.sparse.npoints用法及代码示例
- Python pandas.Series.sparse.from_coo用法及代码示例
- Python pandas.Series.sparse.sp_values用法及代码示例
- Python pandas.Series.sparse.density用法及代码示例
- Python pandas.Series.str.isdecimal用法及代码示例
- Python pandas.Series.str.get用法及代码示例
- Python pandas.Series.sample用法及代码示例
- Python pandas.Series.str.replace用法及代码示例
- Python pandas.Series.str.endswith用法及代码示例
- Python pandas.Series.str.isspace用法及代码示例
- Python pandas.Series.str.isdigit用法及代码示例
- Python pandas.Series.set_axis用法及代码示例
- Python pandas.Series.str.wrap用法及代码示例
- Python pandas.Series.str.isalnum用法及代码示例
- Python pandas.Series.str.zfill用法及代码示例
- Python pandas.Series.set_flags用法及代码示例
- Python pandas.Series.sub用法及代码示例
- Python pandas.Series.str.partition用法及代码示例
- Python pandas.Series.str.isnumeric用法及代码示例
- Python pandas.Series.str.startswith用法及代码示例
注:本文由纯净天空筛选整理自pandas.pydata.org大神的英文原创作品 pandas.Series.sparse.to_coo。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
