我有一个这样定义的列列表:
col_list=['Name_x','Num_x']
我有一个df。任何与 colname 匹配的列都是 col_list,我希望从 col_name 中删除 the_x。
我该怎么做?
最佳答案
# 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')
关于python - 如何在 Pandas 中使用通配符重命名多列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57485023/