2

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
Joachim Spange
  • 85
  • 1
  • 1
  • 10
  • Does this answer your question? [Renaming column names in Pandas](https://stackoverflow.com/questions/11346283/renaming-column-names-in-pandas) – Kins Aug 03 '22 at 08:18
  • `df.rename(columns={0:"C", 1:"D"}, inplace=True)` to `df.rename(columns={'A':"C", 'B':"D"}, inplace=True)` – Ashkan Goleh Pour Aug 03 '22 at 08:24
  • Thank you @AshkanGolehPour, but that does not solve my problem because I'm working with many dataframes and the first column is not always named `A`, but I do know that the first column should be re-named to `C` – Joachim Spange Aug 03 '22 at 08:48
  • Thank you @Kins, but no. I read it and I cannot see that it helps my case of renaming column names by integer position and not name (E.G. renaming the first column to `C` with specifying the name of the first column) – Joachim Spange Aug 03 '22 at 09:02

1 Answers1

2

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
Umar.H
  • 22,559
  • 7
  • 39
  • 74
  • 2
    Nice use of `zip`. Note that you can generalize to any column position with `dict(zip(df.iloc[:, [3,7]].columns, ['C','E']))` (for columns 3, 7). (+1) – mozway Aug 03 '22 at 08:54
  • `df.rename(columns=dict(zip(df.columns, ['C','E'])), inplace=True)` work, but then I would, personally, prefer `df.columns = ["C", "D"]` however my question is still unanswered. Is it possible to use `df.rename` to change the name of column number `X` to `new_name` – Joachim Spange Aug 03 '22 at 09:08
  • @mozway the generalized expression `df.rename(columns=dict(zip(df.iloc[:, [0,1]].columns, ['C','D'])), inplace=True)` woks nice. – Joachim Spange Aug 03 '22 at 09:14