【Pandas】pandas Series dropna
Pandas2.2 Series
Computations descriptive stats
方法 | 描述 |
---|---|
Series.backfill(*[, axis, inplace, limit, …]) | 用于填充 Series 中缺失值(NaN)的方法 |
Series.bfill(*[, axis, inplace, limit, …]) | 用于填充 Series 中缺失值(NaN)的方法 |
Series.dropna(*[, axis, inplace, how, …]) | 用于删除 Series 中包含缺失值(NaN)的元素的方法 |
pandas.Series.dropna
pandas.Series.dropna
是用于删除 Series
中包含缺失值(NaN)的元素的方法。它可以根据不同的参数设置来灵活处理缺失值。
参数说明
- axis:{0或’index’},默认为0。表示沿哪个轴进行操作。对于
Series
来说,这个参数通常不需要设置。 - inplace:布尔值,默认为
False
。如果为True
,则就地修改原Series
,否则返回一个新的Series
。 - how:可选,默认为
None
。指定如何判断缺失值:'any'
:只要有缺失值就删除该元素。'all'
:只有当所有值都是缺失值时才删除该元素。对于Series
来说,这个参数通常没有实际意义,因为Series
只有一列。
- ignore_index:布尔值,默认为
False
。如果为True
,则忽略原始索引,重置为整数索引。
示例及结果
示例1:基本用法
import pandas as pd
import numpy as np
# 创建一个包含缺失值的 Series
s = pd.Series([1, 2, np.nan, 4, np.nan, 6])
print("原始 Series:")
print(s)
# 使用 dropna 方法删除缺失值
cleaned_s = s.dropna()
print("\n删除缺失值后的 Series (使用 dropna):")
print(cleaned_s)
输出结果:
原始 Series:
0 1.0
1 2.0
2 NaN
3 4.0
4 NaN
5 6.0
dtype: float64
删除缺失值后的 Series (使用 dropna):
0 1.0
1 2.0
3 4.0
5 6.0
dtype: float64
示例2:使用 inplace
参数
# 创建一个包含缺失值的 Series
s_inplace = pd.Series([1, 2, np.nan, 4, np.nan, 6])
print("原始 Series:")
print(s_inplace)
# 使用 dropna 方法并设置 inplace=True
s_inplace.dropna(inplace=True)
print("\n删除缺失值后的 Series (使用 dropna 并设置 inplace=True):")
print(s_inplace)
输出结果:
原始 Series:
0 1.0
1 2.0
2 NaN
3 4.0
4 NaN
5 6.0
dtype: float64
删除缺失值后的 Series (使用 dropna 并设置 inplace=True):
0 1.0
1 2.0
3 4.0
5 6.0
dtype: float64
示例3:使用 ignore_index
参数
# 创建一个包含缺失值的 Series
s_ignore_index = pd.Series([1, 2, np.nan, 4, np.nan, 6])
print("原始 Series:")
print(s_ignore_index)
# 使用 dropna 方法并设置 ignore_index=True
cleaned_s_ignore_index = s_ignore_index.dropna(ignore_index=True)
print("\n删除缺失值后的 Series (使用 dropna 并设置 ignore_index=True):")
print(cleaned_s_ignore_index)
输出结果:
原始 Series:
0 1.0
1 2.0
2 NaN
3 4.0
4 NaN
5 6.0
dtype: float64
删除缺失值后的 Series (使用 dropna 并设置 ignore_index=True):
0 1.0
1 2.0
2 4.0
3 6.0
dtype: float64
示例4:使用 how
参数
由于 Series
只有一列,how
参数的效果在 Series
上并不明显,但在 DataFrame
中有显著区别。为了完整性,这里也给出示例:
# 创建一个包含缺失值的 Series
s_how = pd.Series([1, 2, np.nan, 4, np.nan, 6])
print("原始 Series:")
print(s_how)
# 使用 dropna 方法并设置 how='any'
cleaned_s_how_any = s_how.dropna(how='any')
print("\n删除缺失值后的 Series (使用 dropna 并设置 how='any'):")
print(cleaned_s_how_any)
# 使用 dropna 方法并设置 how='all'
cleaned_s_how_all = s_how.dropna(how='all')
print("\n删除缺失值后的 Series (使用 dropna 并设置 how='all'):")
print(cleaned_s_how_all)
输出结果:
原始 Series:
0 1.0
1 2.0
2 NaN
3 4.0
4 NaN
5 6.0
dtype: float64
删除缺失值后的 Series (使用 dropna 并设置 how='any'):
0 1.0
1 2.0
3 4.0
5 6.0
dtype: float64
删除缺失值后的 Series (使用 dropna 并设置 how='all'):
0 1.0
1 2.0
3 4.0
5 6.0
dtype: float64
通过这些示例,可以看到 dropna
方法在不同参数下的使用方式及其效果。特别是 inplace
和 ignore_index
参数可以更灵活地控制删除行为,从而更好地处理缺失值。