Alex Kinman Alex Kinman - 1 year ago 174
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
dollars dollars
0 0.7085 0.5000

I want to rename it so that I have:

In [77]: X_R
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 Source
X_R.columns = ['Retail','Cost']
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download