Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

pandas

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.

like image 425
Dennix Avatar asked Oct 30 '25 00:10

Dennix


1 Answers

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
like image 145
anky Avatar answered Nov 01 '25 15:11

anky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!