The Python Oracle

Is there a built in Python/pandas function to rename duplicate columns in a pandas.DataFrame?

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Underwater World

--

Chapters
00:00 Is There A Built In Python/Pandas Function To Rename Duplicate Columns In A Pandas.Dataframe?
00:37 Accepted Answer Score 7
00:56 Thank you

--

Full question
https://stackoverflow.com/questions/6361...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #pandas

#avk47



ACCEPTED ANSWER

Score 7


but I could not find a handy way to do this to a DataFrame object already

To an existing dataframe we have to resort to some code, there is no builtin;

s = pd.Series(df.columns)
df.columns= df.columns+s.groupby(s).cumcount().replace(0,'').astype(str)

   x  x1  y
0  2   5  1
1  1   9  3
2  4   1  2