python - 如何在 Pandas 中使用通配符重命名多列

标签 python pandas

我有一个这样定义的列列表:

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/

相关文章:

python - 在 python/pandas 数据框中使用 group by 函数

Python pandas 用不同的名称保存多个 CSV

python - 如果文件名为 email.py 或 logging.py,则无法使用 Python Requests 模块?

python - 使用索引将一个数据帧的值映射到另一数据帧列

python - 用字符串替换 'match' 对象,Python

python - 计数如果 : job is in a certain time interval

python - 如何过滤掉python中的单词?

python - 如何使用 pyparsing 对放置在不同位置的标记进行分组?

python - 相当于 Pandas 的numpy

python - Python 中数据透视表列之间的差异