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 对象的方法 |
Series.reindex_like(other[, method, copy, …]) | 用于将 Series 对象重新索引以匹配另一个 Series 或 DataFrame 的索引的方法 |
Series.rename([index, axis, copy, inplace, …]) | 用于重命名 Series 对象的索引或轴标签的方法 |
Series.rename_axis([mapper, index, axis, …]) | 用于为 Series 的索引轴(index )或列轴(columns ,对于 Series 通常不适用)设置名称 |
Series.reset_index([level, drop, name, …]) | 用于将 Series 的索引重置为默认整数索引的方法 |
Series.sample([n, frac, replace, weights, …]) | 用于从 Series 中随机抽取样本的方法 |
Series.set_axis(labels, *[, axis, copy]) | 用于设置 Series 对象的索引标签的方法。 |
Series.take(indices[, axis]) | 用于根据指定的位置索引(indices )从 Series 中提取元素 |
Series.tail([n] ) | 用于返回 Series 的最后 n 行数据 |
Series.truncate([before, after, axis, copy]) | 用于截断 Series 或 DataFrame 的数据 |
Series.where(cond[, other, inplace, axis, level]) | 用于条件过滤和替换的函数 |
pandas.Series.where
pandas.Series.where
是 Pandas 库中用于条件过滤和替换的函数。它会根据给定的条件 cond
来保留满足条件的数据,对于不满足条件的数据可以进行替换或设置为缺失值(NaN)。该方法的具体参数如下:
- cond: 一个布尔 Series 或者一个返回布尔值的条件表达式,用于指定哪些元素应该被保留。
- other (可选): 当条件不满足时,用什么值来替换,默认是 NaN。
- inplace (可选): 是否在原 Series 上进行修改,默认是 False。
- axis (可选): 沿着哪个轴应用条件,对于 Series 可以忽略此参数。
- level (可选): 如果有 MultiIndex,可以在特定级别上应用条件。
示例及结果
示例 1: 基本用法
import pandas as pd# 创建一个简单的 Series
s = pd.Series([1, 2, 3, 4, 5])# 使用 where 方法,保留大于 2 的值,其余位置用 -1 替换
result = s.where(s > 2, -1)print("原始 Series:")
print(s)
print("\n使用 where 方法后的 Series:")
print(result)
输出结果:
原始 Series:
0 1
1 2
2 3
3 4
4 5
dtype: int64使用 where 方法后的 Series:
0 -1
1 -1
2 3
3 4
4 5
dtype: int64
示例 2: 在原地修改
# 使用 inplace 参数直接在原 Series 上修改
s.where(s > 2, -1, inplace=True)print("修改后的 Series:")
print(s)
输出结果:
修改后的 Series:
0 -1
1 -1
2 3
3 4
4 5
dtype: int64
示例 3: 处理缺失值
# 创建一个包含 NaN 的 Series
s_with_nan = pd.Series([1, 2, None, 4, 5])# 使用 where 方法,保留非 NaN 的值,其余位置用 0 替换
result_with_nan = s_with_nan.where(pd.notna(s_with_nan), 0)print("原始 Series 包含 NaN:")
print(s_with_nan)
print("\n使用 where 方法处理 NaN 后的 Series:")
print(result_with_nan)
输出结果:
原始 Series 包含 NaN:
0 1.0
1 2.0
2 NaN
3 4.0
4 5.0
dtype: float64使用 where 方法处理 NaN 后的 Series:
0 1.0
1 2.0
2 0.0
3 4.0
4 5.0
dtype: float64
通过这些示例可以看到,pandas.Series.where
是一个非常灵活且强大的工具,适用于多种条件过滤和数据替换的场景。