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
 
    