简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM