有条件地重命名多个列名

时间:2019-11-20 11:55:13

标签: python pandas

我想通过为每个重复项添加数字1重命名具有相同名称的多个列。

这是我尝试过的

select_cols = np.asarray([i for i, col in enumerate(fully_merged.columns) if 'stance' in col])
fully_merged.rename(columns={cols:'stance'+str(i) for cols in fully_merged.columns[select_cols] for i in range(1,7)})
# df.rename(columns={col: '' for col in df.columns[idx_filter]}, inplace=True)

但是对于每一个带有单词stance的列,它都会返回'stance6'

以下是我的列名示例:

x1,stance,y1,x2,stance,y2,x3,stance,y3,x4,stance,y4,x5,stance,y5,x6,stance,y6

预期输出:

x1,stance1,y1,x2,stance2,y2,x3,stance3,y3,x4,stance4,y4,x5,stance5,y5,x6,stance6,y6

1 个答案:

答案 0 :(得分:1)

将列转换为Series并通过GroupBy.cumcount创建计数器,该操作用于将值从1N的列进行重命名:

a = 'x1,stance,y1,x2,stance,y2,x3,stance,y3,x4,stance,y4,x5,stance,y5,x6,stance,y6'
df = pd.DataFrame(columns=a.split(','))

select_cols = df.columns.to_series()
count = select_cols.groupby(level=0).cumcount().add(1).astype(str)

df.columns = np.where(select_cols == 'stance', 'stance' + count, select_cols)
print (df)
Empty DataFrame
Columns: [x1, stance1, y1, x2, stance2, y2, x3, stance3, y3, 
          x4, stance4, y4, x5, stance5, y5, x6, stance6, y6]
Index: []