1>>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
2>>> df.rename(columns={"A": "a", "B": "c"})
3 a c
40 1 4
51 2 5
62 3 6
7
1df_new = df.rename(columns={'A': 'a'}, index={'ONE': 'one'})
2print(df_new)
3# a B C
4# one 11 12 13
5# TWO 21 22 23
6# THREE 31 32 33
7
8print(df)
9# A B C
10# ONE 11 12 13
11# TWO 21 22 23
12# THREE 31 32 33
13
1>>> df.rename(columns={"A": "a", "B": "b", "C": "c"}, errors="raise")
2Traceback (most recent call last):
3KeyError: ['C'] not found in axis
4
1>>> df.index
2RangeIndex(start=0, stop=3, step=1)
3>>> df.rename(index=str).index
4Index(['0', '1', '2'], dtype='object')
5