【问题标题】:Pandas replace a character in all column namesPandas 替换所有列名中的一个字符
【发布时间】:2016-09-28 08:00:31
【问题描述】:

我的数据框的列名(来自 .csv 文件)包含 (),我想用 _ 替换它们。

我怎样才能为所有列做到这一点?

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    使用str.replace:

    df.columns = df.columns.str.replace("[()]", "_")
    

    示例:

    df = pd.DataFrame({'(A)':[1,2,3],
                       '(B)':[4,5,6],
                       'C)':[7,8,9]})
    
    print (df)
       (A)  (B)  C)
    0    1    4   7
    1    2    5   8
    2    3    6   9
    
    df.columns = df.columns.str.replace(r"[()]", "_")
    print (df)
       _A_  _B_  C_
    0    1    4   7
    1    2    5   8
    2    3    6   9
    

    【讨论】:

    • @Seymour 表示部分或全部数字列,所以需要df.columns = df.columns.astype(str).str.replace("[()]", "_")
    • @Seymour - 我认为需要检查this solutions
    • 很好的答案。谢谢。只是好奇为什么'[ ]' 用于"[()]" 部分?我试过了,它不起作用。你能告诉我[ ] 在这种情况下做了什么吗?
    • @BowenLiu - 表示 regex 仅匹配 ()
    • 非常感谢。我见过人们编写可以执行复杂任务的简短而优雅的正则表达式。我正在努力学习它。但是,那里有很多教程,我感到困惑。您提供的链接是否是阅读该主题的良好来源?
    【解决方案2】:

    旧版 pandas 不适用于上述已接受的答案。需要这样的东西:

    df.columns = [c.replace("[()]", "_") for c in list(df.columns)]
    

    【讨论】:

      【解决方案3】:

      方括号用于划分要提取的字符范围。例如:

      r"[Nn]ational"
      

      将提取我们有“国家”和“国家”的两个出现,即它提取 N 或 n。

      【讨论】:

        猜你喜欢
        • 2021-07-02
        • 2018-07-11
        • 2018-05-28
        • 2018-10-27
        • 2020-01-27
        • 2021-10-27
        • 2021-10-19
        • 1970-01-01
        • 2020-10-02
        相关资源
        最近更新 更多