【问题标题】:pandas:how to change all column names with erasing specific letters [duplicate]熊猫:如何通过删除特定字母来更改所有列名[重复]
【发布时间】:2019-05-06 19:33:11
【问题描述】:

我正在使用 Anaconda 从 hive 中读取表,并且我的所有列名都自动添加了前缀,例如 test.age,test.sex,test.degree... 如何使用 pandas 擦除所有前缀 'test.'?

【问题讨论】:

  • 您没有提供任何证据证明您已尝试解决此问题,这就是我没有提供答案的原因。我建议你看看pandas.DataFrame.columns属性
  • 请提供您尝试过的内容。

标签: python pandas


【解决方案1】:

一种方法是:

df.columns = [i.replace("test.", "") for i in df.columns]

【讨论】:

    【解决方案2】:
    df.columns = [i[1] for i in df.columns.str.split('.')]
    

    【讨论】:

      猜你喜欢
      • 2019-08-13
      • 2017-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-30
      • 1970-01-01
      • 2019-11-08
      • 1970-01-01
      相关资源
      最近更新 更多