Pandas2.2 Series
Computations descriptive stats
方法 | 描述 |
---|---|
Series.align(other[, join, axis, level, …]) | 用于将两个 Series 对齐,使其具有相同的索引 |
Series.case_when(caselist) | 用于根据条件列表对 Series 中的元素进行条件判断并返回相应的值 |
Series.drop([labels, axis, index, columns, …]) | 用于从 Series 中删除指定的行或列(对于 Series 来说,通常是删除行) |
Series.droplevel(level[, axis]) | 用于从多层索引(MultiIndex)的 Series 中删除指定的索引层级 |
Series.drop_duplicates(*[, keep, inplace, …]) | 用于从 Series 中删除重复的值 |
Series.duplicated([keep] ) |
用于检测 Series 中的重复值 |
Series.equals(other) | 用于比较两个 Series 对象是否完全相等的方法 |
Series.first(offset) | 用于根据日期偏移量(offset )选择 Series 中时间序列数据的初始部分 |
Series.head([n] ) |
用于返回 Series 的前 n 个元素 |
Series.idxmax([axis, skipna]) | 用于返回 Series 中最大值的索引 |
Series.idxmin([axis, skipna]) | 用于返回 Series 中最小值的索引 |
Series.isin(values) | 用于检查 Series 中的每个元素是否存在于给定的值集合 values 中 |
Series.last(offset) | 用于根据日期偏移量(offset )选择 Series 中时间序列数据的末尾部分 |
Series.reindex([index, axis, method, copy, …]) | 用于重新索引 Series 对象的方法 |
pandas.Series.reindex
pandas.Series.reindex
是一个用于重新索引 Series
对象的方法,允许用户根据新的索引对数据进行重排或填充。以下是该方法的参数说明:
- index: 新的索引标签列表。
- axis: 仅在 DataFrame 中有意义,默认为 0(行),对于 Series 可忽略。
- method: 指定重新索引时使用的填充方法,如 ‘backfill’、‘bfill’、‘pad’、‘ffill’ 等。
- copy: 如果为 True,则即使新旧索引相同也会返回一个新的副本,默认为 True。
- level: 如果索引是 MultiIndex,则指定使用哪一级别进行重新索引。
- fill_value: 用于填充缺失值的值,默认为 NaN。
- limit: 使用填充方法时的最大填充距离。
- tolerance: 最大容差,超出此范围则不填充。
示例及结果
import pandas as pd
# 创建一个简单的 Series
s = pd.Series([1, 2, 3], index=['a', 'b', 'c'])
# 新的索引
new_index = ['a', 'b', 'c', 'd']
# 使用 reindex 方法
s_reindexed = s.reindex(new_index, fill_value=0)
print(s_reindexed)
输出结果
a 1
b 2
c 3
d 0
dtype: int64
在这个例子中,原始 Series
的索引是 ['a', 'b', 'c']
,我们通过 reindex
方法将其重新索引为 ['a', 'b', 'c', 'd']
。由于 'd'
在原始 Series
中不存在,因此我们使用 fill_value=0
来填充这个缺失值。