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 通常不适用)设置名称 |
pandas.Series.rename_axis
pandas.Series.rename_axis()
是 Pandas 库中的一个方法,用于为 Series 的索引轴(index
)或列轴(columns
,对于 Series 通常不适用)设置名称。它可以修改索引轴或列轴的名称,而不是修改索引或列本身的值。
方法签名
Series.rename_axis(mapper=<no_default>, *, index=<no_default>, axis=0, copy=True, inplace=False)
参数详解
-
mapper
:- 用于设置轴名称的值。
- 可以是标量(如字符串)或字典、函数等。
- 如果未指定
index
参数,则mapper
用于设置索引轴的名称。 - 默认值:
<no_default>
。
-
index
:- 用于设置索引轴名称的值。
- 可以是标量(如字符串)或字典、函数等。
- 如果同时指定了
mapper
和index
,则index
优先。 - 默认值:
<no_default>
。
-
axis
:- 指定要操作的轴。
- 对于 Series,
axis
只能是0
(默认值),表示操作索引轴。 - 默认值:
0
。
-
copy
:- 是否返回一个新的 Series。
- 如果
True
(默认值),则返回一个新的 Series。 - 如果
False
,则尝试直接修改原 Series(可能不会总是生效)。 - 默认值:
True
。
-
inplace
:- 是否直接修改原 Series。
- 如果
True
,则直接修改原 Series,并返回None
。 - 如果
False
(默认值),则返回一个新的 Series。 - 默认值:
False
。
返回值
- 如果
inplace=False
(默认),则返回一个新的 Series,其中索引轴或列轴的名称被修改。 - 如果
inplace=True
,则直接修改原 Series,并返回None
。
示例及结果
示例 1:为索引轴设置名称
import pandas as pd
# 创建一个 Series
s = pd.Series([10, 20, 30], index=['a', 'b', 'c'])
print("原 Series:")
print(s)
# 为索引轴设置名称
result = s.rename_axis('index_name')
print("\n修改索引轴名称后的 Series:")
print(result)
结果:
原 Series:
a 10
b 20
c 30
dtype: int64
修改索引轴名称后的 Series:
index_name
a 10
b 20
c 30
dtype: int64
解释:
- 索引轴的名称被设置为
'index_name'
。
示例 2:使用 index
参数设置索引轴名称
import pandas as pd
# 创建一个 Series
s = pd.Series([10, 20, 30], index=['a', 'b', 'c'])
print("原 Series:")
print(s)
# 使用 index 参数为索引轴设置名称
result = s.rename_axis(index='index_label')
print("\n修改索引轴名称后的 Series:")
print(result)
结果:
原 Series:
a 10
b 20
c 30
dtype: int64
修改索引轴名称后的 Series:
index_label
a 10
b 20
c 30
dtype: int64
解释:
- 索引轴的名称被设置为
'index_label'
。
示例 3:使用 inplace=True
直接修改原 Series
import pandas as pd
# 创建一个 Series
s = pd.Series([10, 20, 30], index=['a', 'b', 'c'])
print("原 Series:")
print(s)
# 直接修改原 Series 的索引轴名称
s.rename_axis('index_name', inplace=True)
print("\n修改后的 Series:")
print(s)
结果:
原 Series:
a 10
b 20
c 30
dtype: int64
修改后的 Series:
index_name
a 10
b 20
c 30
dtype: int64
解释:
- 原 Series 的索引轴名称被直接修改为
'index_name'
。
示例 4:移除索引轴名称
import pandas as pd
# 创建一个带名称索引轴的 Series
s = pd.Series([10, 20, 30], index=['a', 'b', 'c'])
s = s.rename_axis('index_name')
print("原 Series:")
print(s)
# 移除索引轴名称
result = s.rename_axis(None)
print("\n移除索引轴名称后的 Series:")
print(result)
结果:
原 Series:
index_name
a 10
b 20
c 30
dtype: int64
移除索引轴名称后的 Series:
a 10
b 20
c 30
dtype: int64
解释:
- 通过传递
None
,移除了索引轴的名称。
注意事项
rename_axis()
仅修改轴名称,不会修改索引或列的值。- 如果
inplace=True
,则直接修改原 Series,并返回None
。 - 可以通过传递
None
移除轴名称。
通过 rename_axis()
方法,可以方便地为 Series 的索引轴设置或修改名称,使数据更具可读性和结构化。