如何在pandas中使用通配符重命名多个列

3
我有一个像这样定义的列列表:
col_list=['Name_x','Num_x']

我有一个数据框df. 如果任何一列与colname匹配,那么我希望从这个列名中删除_the_x。
我应该如何做?
1个回答

1
# df.columns
# Index(['Name_x', 'Num_x', 'test_x'], dtype='object')

col_list=['Name_x','Num_x']

df.columns = np.where(
    df.columns.isin(col_list), df.columns.str.replace(r'_x$', ''), df.columns)

# df.columns
# Index(['Name', 'Num', 'test_x'], dtype='object')

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接