Python Pandas,如何在所有以数字开头的列名中添加前缀?

Python Pandas,如何在所有以数字开头的列名中添加前缀?,python,pandas,Python,Pandas,我有一个包含很多列的数据集,超过400列,其中一些在列名的开头有数字,下面是列名的部分列表: 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_

我有一个包含很多列的数据集,超过400列,其中一些在列名的开头有数字,下面是列名的部分列表:

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
如何将前缀附加到所有以数字开头的列?应该是这样的:

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
我无法单独重命名它们,因为列太多。

使用正则表达式:

df.columns = df.columns.str.replace(r"^(\d+)", r"col_\1")
如果它以(
^
)一个或多个数字(
\d+
)开头,则捕获这些数字并将其替换为
列{digits}
,其中
\1
表示第一个捕获组(如果有)