Emm
Emm

Reputation: 2507

Conditionally renaming multiple column names

I would like to rename multiple columns with the same name by appending the number 1 for each duplicate.

This is what I have tried

select_cols = np.asarray([i for i, col in enumerate(fully_merged.columns) if 'stance' in col])
fully_merged.rename(columns={cols:'stance'+str(i) for cols in fully_merged.columns[select_cols] for i in range(1,7)})
# df.rename(columns={col: '' for col in df.columns[idx_filter]}, inplace=True)

but this returns 'stance6' for every column with the word stance

Here is a sample of my column names:

x1,stance,y1,x2,stance,y2,x3,stance,y3,x4,stance,y4,x5,stance,y5,x6,stance,y6

The expected output:

x1,stance1,y1,x2,stance2,y2,x3,stance3,y3,x4,stance4,y4,x5,stance5,y5,x6,stance6,y6

Upvotes: 1

Views: 172

Answers (1)

jezrael
jezrael

Reputation: 862511

Convert columns to Series and create counter by GroupBy.cumcount what is used for rename columns with 1 to N values:

a = 'x1,stance,y1,x2,stance,y2,x3,stance,y3,x4,stance,y4,x5,stance,y5,x6,stance,y6'
df = pd.DataFrame(columns=a.split(','))

select_cols = df.columns.to_series()
count = select_cols.groupby(level=0).cumcount().add(1).astype(str)

df.columns = np.where(select_cols == 'stance', 'stance' + count, select_cols)
print (df)
Empty DataFrame
Columns: [x1, stance1, y1, x2, stance2, y2, x3, stance3, y3, 
          x4, stance4, y4, x5, stance5, y5, x6, stance6, y6]
Index: []

Upvotes: 1

Related Questions