Home > Blockchain >  df.rename does not alter df column names, but df.columns and df.set_axis do (Pandas)
df.rename does not alter df column names, but df.columns and df.set_axis do (Pandas)

Time:08-04

I have a pandas dataframe that I want to rename the columns on

When I run:

df.rename(columns={0:"C", 1:"D"}, inplace=True)

No change happens, it's still the original column names. But if I do:

df.columns = ["C", "D"]

or

df.set_axis(["C", "D"],axis=1, inplace=True)

It works.

Why does not df.rename work?

NOTE: I specifically would like to rename the first and second column regardless of what their name is, it may change (in my case) so I can't specify it.

Example:

df = pd.DataFrame({"A": pd.Series(range(0,2)),"B": pd.Series(range(2,4))})
df
    A   B
1   0   2
2   1   3

df = pd.DataFrame({"A": pd.Series(range(0,2)),"B": pd.Series(range(2,4))})
df.rename(columns={0:"C", 1:"D"}, inplace=True)
df
    A   B
1   0   2
2   1   3

df = pd.DataFrame({"A": pd.Series(range(0,2)),"B": pd.Series(range(2,4))})
df.columns = ["C", "D"]
df
    C   D
0   0   2
1   1   3

df = pd.DataFrame({"A": pd.Series(range(0,2)),"B": pd.Series(range(2,4))})
df.set_axis(["C", "D"],axis=1, inplace=True)
df
    C   D
0   0   2
1   1   3

EDIT:

My original dataframe had the column names 0 and 1 which is why df.rename(columns={0:"C", 1:"D"}, inplace=True) worked.

Example:

df = pd.DataFrame([range(2,4), range(4,6)])
df
    0   1
0   2   3
1   4   5

df.rename(columns={0:"C", 1:"D"}, inplace=True)
df
    C   D
0   2   3
1   4   5

CodePudding user response:

If you don't want to rename by using the old name, you could zip the current columns and pass in the number of items you want.

If you're using Python 3.7 then order should be preserved

Also don't use inplace=True

print(df)

   A  B
0  0  2
1  1  3

df.rename(columns=dict(zip(df.columns, ['C','E'])))

   C  E
0  0  2
1  1  3

df.rename(columns=dict(zip(df.columns, ['E'])))

   E  B
0  0  2
1  1  3
  • Related