【问题标题】:Selectively updating the multilevel column name of a dataframe (python), a column with a specific text to have a particular name有选择地更新数据框(python)的多级列名称,具有特定文本的列具有特定名称
【发布时间】:2019-07-19 15:06:32
【问题描述】:

我有一个多级列数据框,其中前两列在级别 1 中包含文本(级别 2 为空),其余列在级别 2 中包含日期(级别 1 文本)。

S1  S2        _id                  _id
              07-08-2016 00:00     14-08-2016 00:00
ABC ABC1      22071                19474
CDE CDE1      17                   3

我尝试了类似的方法,但它不起作用

pd.MultiIndex.from_tuples([(s1,s1) if isinstance(s1, datetime.date)  else (s2) for s1,s2 in df.columns],names=.columns.names)

【问题讨论】:

    标签: python-3.x multiple-columns rename multi-index


    【解决方案1】:

    我尝试了另一种方法

    df.columns = [(s2) if s1 == '_id' else (s1) for s1,s2 in df.columns]
    

    我在字符串上应用了 if 条件,但理想的解决方案是在多级列名中识别日期格式

    【讨论】:

      猜你喜欢
      • 2022-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多