'Trying to rename repeated columns in pandas DataFrame
I have a list of duplicate columns in my DataFrame (named as df
here in the code) and I am trying to rename duplicate columns by adding a number in the end. For example:
If there are two columns named col
, then I want two columns named col1
and col2
.
But this code gives two column names as col1
and col1
again.
for col in duplicate_col:
counter = 1
for col2 in df.columns:
if(col2==col):
df.rename(columns={col2:col2+str(counter)},inplace=True)
counter = counter+1
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|