Panda's DataFrame - renaming multiple identically named columns
pandas, python
Solution
I was looking to find a solution within Pandas more than a general Python solution. Column's get_loc() function returns a masked array if it finds duplicates with 'True' values pointing to the locations where duplicates are found. I then use the mask to assign new values into those locations. In my case, I know ahead of time how many dups I'm going to get and what I'm going to assign to them but it looks like df.columns.get_duplicates() would return a list of all dups and you can then use that list in conjunction with get_loc() if you need a more generic dup-weeding action
'''UPDATED AS-OF SEPT 2020'''
cols=pd.Series(df.columns)
for dup in df.columns[df.columns.duplicated(keep=False)]:
cols[df.columns.get_loc(dup)] = ([dup + '.' + str(d_idx)
if d_idx != 0
else dup
for d_idx in range(df.columns.get_loc(dup).sum())]
)
df.columns=cols
blah blah2 blah3 blah.1 blah.2
0 0 1 2 3 4
1 5 6 7 8 9
New Better Method (Update 03Dec2019)
This code below is better than above code. Copied from another answer below (@SatishSK):
#sample df with duplicate blah column
df=pd.DataFrame(np.arange(2*5).reshape(2,5))
df.columns=['blah','blah2','blah3','blah','blah']
df
# you just need the following 4 lines to rename duplicates
# df is the dataframe that you want to rename duplicated columns
cols=pd.Series(df.columns)
for dup in cols[cols.duplicated()].unique():
cols[cols[cols == dup].index.values.tolist()] = [dup + '.' + str(i) if i != 0 else dup for i in range(sum(cols == dup))]
# rename the columns with the cols list.
df.columns=cols
df
Output:
blah blah2 blah3 blah.1 blah.2
0 0 1 2 3 4
1 5 6 7 8 9
Problem
I have several columns named the same in a df. I need to rename them but the problem is that the `df.rename` method renames them all the same way. How I can rename the below blah(s) to blah1, blah4, blah5? ``` df = pd.DataFrame(np.arange(2*5).reshape(2,5)) df.columns = ['blah','blah2','blah3','blah','blah'] df # blah blah2 blah3 blah blah # 0 0 1 2 3 4 # 1 5 6 7 8 9 ``` Here is what happens when using the `df.rename` method: ``` df.rename(columns={'blah':'blah1'}) # blah1 blah2 blah3 blah1 blah1 # 0 0 1 2 3 4 # 1 5 6 7 8 9 ```