Alex Kinman Alex Kinman - 8 days ago 7
Python Question

Renaming columns in a Pandas dataframe with duplicate column names?

I have a df X with columns with duplicate names:

In [77]: X_R
Out[77]:
dollars dollars
0 0.7085 0.5000


I want to rename it so that I have:

In [77]: X_R
Out[77]:
Retail Cost
0 0.7085 0.5000


Using the Pandas rename function does' work:

X_R.rename(index=str, columns={"dollars": "Retail", "dollars": "Cost"})


Just gives me two columns named Cost.

How can I rename the columns in this case?

Answer
X_R.columns = ['Retail','Cost']
Comments