【问题标题】:Renaming the columns in pd.DataFrame based on the adjacent column name根据相邻列名重命名 pd.DataFrame 中的列
【发布时间】:2021-01-23 05:46:24
【问题描述】:

我的 csv 文件如下图所示。

所以我想使用相邻的列 slice-0010-EDSR_x2 重命名列 X。 所以新的列 X 名称应该是 slice-0010-EDSR_x2_X 并且此列 slice-0010-EDSR_x2 名称应为 slice-0010-EDSR_x2_Y . 并对应所有其他列

这可能吗?

【问题讨论】:

    标签: python pandas dataframe multiple-columns rename


    【解决方案1】:

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

    【讨论】:

    • ValueError: could not broadcast input array from shape (8) into shape (9) 给出了这个错误
    【解决方案2】:

    如果我有这样的示例数据:

    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  
    

    【讨论】:

      猜你喜欢
      • 2019-05-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-05
      • 2019-04-07
      • 2020-10-03
      • 2021-02-03
      • 2018-12-21
      • 2021-04-23
      相关资源
      最近更新 更多