Bill Software Engineer
Bill Software Engineer

Reputation: 7812

Pandas, how do I append a prefix to all column name that start with a number?

I have a dataset with a lot of columns, more than 400, some of them have numbers at the beginning for column names, here is an partial list of column names:

nextday_open
nextday_movement
nextday_movement_direction
nextday_movement_amount
nextday_daytime_movement
nextday_daytime_direction
overnight_movement
overnight_direction
5days_running_std
5days_running_var
5days_diff_std
5days_running_med_diff

How do I append a prefix to all the columns that start with number? It should look like this:

nextday_open
nextday_movement
nextday_movement_direction
nextday_movement_amount
nextday_daytime_movement
nextday_daytime_direction
overnight_movement
overnight_direction
col_5days_running_std
col_5days_running_var
col_5days_diff_std
col_5days_running_med_diff

I can't rename them individually as there are too many columns.

Upvotes: 3

Views: 537

Answers (1)

Mustafa Aydın
Mustafa Aydın

Reputation: 18315

With a regex:

df.columns = df.columns.str.replace(r"^(\d+)", r"col_\1")

If it starts with (^) one or more digits (\d+), capture those digits and replace it with col_{digits} where \1 refers to the first capturing group, if any.

Upvotes: 7

Related Questions