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

如何从只留下一个实例的数据帧中删除特定的重复列

  •  0
  • Vondoe79  · 技术社区  · 7 年前

    我知道上述可能是一个简单的任务,但它已经花了我很多小时没有任何成功。到目前为止,我在网上搜索也没有成功。因此,我有以下数据帧:

    enter image description here

    我希望得到以下预期结果:

    enter image description here

    我使用了以下代码行:

    # drop repeat columns leaving the first instance only:
     df_combo = df_combo.drop_duplicates(['Asgn Carr Cd','Fiscal Month'], keep='first')
    

    然而,这并没有达到我想要的结果。提前感谢您的帮助/建议。

    2 回复  |  直到 7 年前
        1
  •  1
  •   BENY    7 年前

    核对 groupby 具有 first

    df.groupby(level=0,axis=1).first()
    
        2
  •  0
  •   ALollz    7 年前

    可以使用 np.unique

    import numpy as np
    
    df.iloc[:, np.unique(df.columns, return_index=True)[1]]