简体   繁体   English

根据相邻的列名重命名 pd.DataFrame 中的列

[英]Renaming the columns in pd.DataFrame based on the adjacent column name

My csv file looks like below image.我的 csv 文件如下图所示。

So I want to rename the column X using the adjacent column slice-0010-EDSR_x2 .所以我想使用相邻的列slice-0010-EDSR_x2重命名列X。 So the new column X name should be slice-0010-EDSR_x2_X And this column slice-0010-EDSR_x2 name should be slice-0010-EDSR_x2_Y .因此,新列 X 名称应为slice-0010-EDSR_x2_X而此列 slice-0010-EDSR_x2 名称应为slice-0010-EDSR_x2_Y And cooresponding to all other columns并响应所有其他列

Is this thing possible?这件事可能吗?

在此处输入图像描述

If I have sample data like this:如果我有这样的样本数据:

df = pd.DataFrame(
    {
        'Contour': range(5),
        'X': range(5, 10),
        'slice-0010-EDSR_x2': range(10, 15),
        'X_': range(5, 10),
        'slice-0011-EDSR_x2': range(10, 15),        
    }
)

then I can achieve your goal with the following code.那么我可以用下面的代码实现你的目标。

col_names = df.columns.tolist()
new_col_names = []

for i_col, col in enumerate(col_names):
    if i_col == 0:
        new_col = col
    elif col.startswith('X'):
        new_col = col_names[i_col + 1] + '_X'
    else:
        new_col = col + '_Y'
    
    new_col_names.append(new_col)
    
df.columns = new_col_names
print(df)

The result looks like this:结果如下所示:

   Contour  slice-0010-EDSR_x2_X  slice-0010-EDSR_x2_Y  slice-0011-EDSR_x2_X  \
0        0                     5                    10                     5   
1        1                     6                    11                     6   
2        2                     7                    12                     7   
3        3                     8                    13                     8   
4        4                     9                    14                     9   

   slice-0011-EDSR_x2_Y  
0                    10  
1                    11  
2                    12  
3                    13  
4                    14  

You can convert columns to numpy array, because Index does not support mutable operations and set values by positions:您可以将列转换为 numpy 数组,因为 Index 不支持可变操作和按位置设置值:

df = pd.DataFrame(np.random.randint(10, size=(6,5)), 
                  columns=['Contour' ,'X','slice-0011-EDSR' ,'X','slice-1010-EDSR'])
print (df)
   Contour  X  slice-0011-EDSR  X  slice-1010-EDSR
0        0  5                3  5                1
1        2  1                5  6                0
2        4  3                0  7                9
3        9  5                8  4                5
4        0  2                8  6                7
5        5  7                8  9                9

cols = df.columns.to_numpy()
cols[1::2] = cols[2::2] + '_' + 'X'
cols[2::2] = cols[2::2] + '_' + 'Y'
df.columns = cols
print (df) 
   Contour  slice-0011-EDSR_X  slice-0011-EDSR_Y  slice-1010-EDSR_X  \
0        0                  5                  3                  5   
1        2                  1                  5                  6   
2        4                  3                  0                  7   
3        9                  5                  8                  4   
4        0                  2                  8                  6   
5        5                  7                  8                  9   

   slice-1010-EDSR_Y  
0                  1  
1                  0  
2                  9  
3                  5  
4                  7  
5                  9  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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