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

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

我的 csv 文件如下图所示。

所以我想使用相邻的列 slice-0010-EDSR_x2 重命名列 X。 所以新的列 X 名称应该是 slice-0010-EDSR_x2_X 而这一栏 slice-0010-EDSR_x2 名称应该是 slice-0010-EDSR_x2_Y . 并响应所有其他列

这件事可能吗?

您可以将列转换为 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  

如果我有这样的样本数据:

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),        
    }
)

那我可以用下面的代码实现你的目的。

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)

结果如下所示:

   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