Skip to content

Commit f16c244

Browse files
committed
DOC: Enhance DataFrame.rename examples in docstring
1 parent 415830f commit f16c244

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

pandas/core/frame.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5985,12 +5985,31 @@ def rename(
59855985
1 2 5
59865986
2 3 6
59875987
5988-
>>> df.rename({1: 2, 2: 4}, axis="index")
5989-
A B
5990-
0 1 4
5991-
2 2 5
5992-
4 3 6
5993-
"""
5988+
>>> df.rename({1: 2, 2: 4}, axis="index")
5989+
A B
5990+
0 1 4
5991+
2 2 5
5992+
4 3 6
5993+
5994+
**Additional practical example showing index and columns rename:**
5995+
5996+
>>> df = pd.DataFrame({"X": [1, 2, 3], "Y": [4, 5, 6]}, index=["a", "b", "c"])
5997+
>>> df.rename(columns={"X": "new_X", "Y": "new_Y"},
5998+
... index={"a": "row1", "b": "row2", "c": "row3"})
5999+
new_X new_Y
6000+
row1 1 4
6001+
row2 2 5
6002+
row3 3 6
6003+
6004+
**Using a function to transform column names:**
6005+
6006+
>>> df = pd.DataFrame({"X": [1, 2, 3], "Y": [4, 5, 6]}, index=["a", "b", "c"])
6007+
>>> df.rename(columns=str.lower, index=lambda x: x.upper())
6008+
x y
6009+
A 1 4
6010+
B 2 5
6011+
C 3 6
6012+
"""
59946013
self._check_copy_deprecation(copy)
59956014
return super()._rename(
59966015
mapper=mapper,

0 commit comments

Comments
 (0)