代码之家  ›  专栏  ›  技术社区  ›  Cleb

如何基于regex对列重新排序?

  •  1
  • Cleb  · 技术社区  · 7 年前

    假设我有一个这样的数据帧:

    df = pd.DataFrame({'foo':[1, 2], 'bar': [3, 4], 'xyz': [5, 6]})
    
       bar  foo  xyz
    0    3    1    5
    1    4    2    6
    

    我现在想把包含 oo 在第一个位置(即在第0个索引处);始终只有一列具有此模式。

    我现在用 filter 两次和一次 concat :

    pd.concat([df.filter(like='oo'),  df.filter(regex='^((?!(oo)).)*$')], axis=1)
    

    它提供所需的输出:

       foo  bar  xyz
    0    1    3    5
    1    2    4    6
    

    我想知道是否有更有效的方法来做这件事。

    3 回复  |  直到 7 年前
        1
  •  1
  •   Fourier    7 年前

    如何:

    df[sorted(df, key = lambda x: x not in df.filter(like="oo").columns)]
    
        2
  •  3
  •   jezrael    7 年前

    仅使用列表理解,将列表连接在一起并按选择 subset :

    a = [x for x in df.columns if 'oo' in x]
    b = [x for x in df.columns if not 'oo' in x]
    
    df = df[a + b]
    print (df)
       foo  bar  xyz
    0    1    3    5
    1    2    4    6
    
        3
  •  0
  •   Joe    7 年前

    使用 pop :

    cols = list(df)
    col_oo = [col for col in df.columns if 'oo' in col]
    cols.insert(0, cols.pop(cols.index(col_oo[0])))
    df = df.ix[:, cols]
    

    或使用 regex :

    col_oo = [col for col in cols if re.search('oo', col)]