-
Series.rename(index=None, **kwargs)
[source] -
Alter axes input function or functions. Function / dict values must be unique (1-to-1). Labels not contained in a dict / Series will be left as-is. Extra labels listed don?t throw an error. Alternatively, change
Series.name
with a scalar value (Series only).Parameters: index : scalar, list-like, dict-like or function, optional
Scalar or list-like will alter the
Series.name
attribute, and raise on DataFrame or Panel. dict-like or functions are transformations to apply to that axis? valuescopy : boolean, default True
Also copy underlying data
inplace : boolean, default False
Whether to return a new Series. If True then value of copy is ignored.
Returns: renamed : Series (new object)
See also
pandas.NDFrame.rename_axis
Examples
1234567891011121314151617181920212223242526272829303132333435>>> 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
>>> df
=
pd.DataFrame({
"A"
: [
1
,
2
,
3
],
"B"
: [
4
,
5
,
6
]})
>>> df.rename(
2
)
...
TypeError:
'int'
object
is
not
callable
>>> df.rename(index
=
str
, columns
=
{
"A"
:
"a"
,
"B"
:
"c"
})
a c
0
1
4
1
2
5
2
3
6
>>> df.rename(index
=
str
, columns
=
{
"A"
:
"a"
,
"C"
:
"c"
})
a B
0
1
4
1
2
5
2
3
6
Series.rename()

2025-01-10 15:47:30
Please login to continue.