Aliasgher Nooruddin
Aliasgher Nooruddin

Reputation: 563

Pandas Dataframe automatically renames duplicate columns name

I have a dataframe with 10 columns and 160 rows. Column names are based on month and year for e.g Jun'17, July'17, Mar'18 etc. However in excel some columns are repeating like Jun'17 two times When I import them to pandas dataframe it renames duplicate columns to Jun'17 and Jun'17.1

This '.1' is extra and disturbing my whole calculation.

Upvotes: 3

Views: 3095

Answers (1)

Joe
Joe

Reputation: 12417

I dont think it is a good idea have more columns with the same name, and i wouldnt suggest this, but if you want to go with that, you can do in this way:

df = df.rename(columns = {"Jun'17.1":"Jun'17"})

To access to the 2 different columns then do in this way:

df["Jun'17"].iloc[:,0]
df["Jun'17"].iloc[:,1]

Upvotes: 2

Related Questions