Lets say I have a DataFrame with columns with the same name:
x x y
2 5 1
1 9 3
4 1 2 
Is there built-in general function to rename columns with the same name to differ from each other? Example of a possible result:
x0 x1 y
2  5  1
1  9  3
4  1  2 
When reading a csv with pandas.read_csv automatically does this (defaulting mangle_dupe_cols=True) but I could not find a handy way to do this to a DataFrame object already in Python.
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
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With