pandas.Series.rename#
- Series.rename(index=None, *, axis=None, copy=None, inplace=False, level=None, errors='ignore')[源代码]#
更改 Series 索引标签或名称。
函数/字典值必须是唯一的(1 对 1)。不在字典/Series 中的标签将保持不变。列出的额外标签不会引发错误。
或者,更改
Series.name为标量值。更多信息,请参阅 user guide 。
- Parameters:
- index标量、可哈希序列、类似字典或函数,可选
函数或类字典对象是对索引应用的转换。标量或可哈希序列会改变
Series.name属性。- axis{0 或 ‘index’}
未使用。参数是与 DataFrame 兼容性所必需的。
- copybool, default True
同时复制底层数据。
备注
copy 关键字在 pandas 3.0 中将更改行为。Copy-on-Write 将默认启用,这意味着所有带有 copy 关键字的方法都将使用惰性复制机制来延迟复制并忽略 copy 关键字。copy 关键字将在 pandas 的未来版本中移除。
通过启用 copy on write
pd.options.mode.copy_on_write = True,您可以获得未来的行为和改进。- inplacebool,默认 False
是否返回新的 Series。如果为 True,则忽略 copy 的值。
- levelint 或级别名称,默认为 None
对于 MultiIndex,仅重命名指定级别的标签。
- errors{‘ignore’, ‘raise’},默认为 ‘ignore’
如果为 ‘raise’,当类字典映射表或
index包含在正在转换的索引中不存在的标签时,引发 KeyError。如果为 ‘ignore’,将重命名现有键并忽略多余的键。
- Returns:
- Series 或 None
索引标签或名称已更改的 Series,如果
inplace=True则为 None。
参见
DataFrame.rename相应的 DataFrame 方法。
Series.rename_axis设置轴的名称。
Examples
>>> s = pd.Series([1, 2, 3]) >>> s 0 1 1 2 2 3 dtype: int64 >>> s.rename("my_name") # scalar, changes Series.name 0 1 1 2 2 3 Name: my_name, dtype: int64 >>> s.rename(lambda x: x ** 2) # function, changes labels 0 1 1 2 4 3 dtype: int64 >>> s.rename({1: 3, 2: 5}) # mapping, changes labels 0 1 3 2 5 3 dtype: int64