重命名30个数据框列 [英] Renaming 30 dataframes columns

查看:76
本文介绍了重命名30个数据框列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有30个数据框,每个 df 都有一列。列名很大,如下所示:

I have 30 data frames and each df has a column. The column names are big and look something like as given below:

df1.columns =  ['123.ABC_xyz_1.CB_1.S_01.M_01.Pmax']
df2.columns =  ['123.ABC_xyz_1.CB_1.S_01.M_02.Pmax']
..
df30.columns =  ['123.ABC_xyz_1.CB_1.S_01.M_30.Pmax']

我想修剪他们的名字,最后希望他们成为如下所示:

I want to trim their names and I want them finally to be something like as given below:

df1.columns =  ['M1Pmax']
df2.columns =  ['M2Pmax']
..
df30.columns =  ['M30Pmax']

我想到了这样的东西:

df_list = [df1,df2,....,df30]
for i,k in enumerate(df_list):
    df_list[i].columns = [col_name+'_df[i]{}'.format(df_list[i]) for col_name in df_list[i].columns]

但是,我上面的代码无法正常工作。

However, my above code is not working properly.

该怎么做?

推荐答案

您正试图以无法使用的名称使用数据框本身。我假设您正在尝试使用数据框的名称。您也不会缩短代码中的任何内容,而只是缩短它的长度。我会建议类似的东西:

You are trying to use the dataframe itself in the name which is not gonna work. I am assuming you were trying to use the name of the dataframe. You are also not shortening anything in your code but just making it longer. I would suggest something like:

df_list = [df1,df2,....,df30]
for i, k in enumerate(df_list):
    df_list[i].columns = ['M{}_'.format(i)+col_name.split(".")[-1] for col_name in df_list[i].columns]

这篇关于重命名30个数据框列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆