Combine duplicated columns within a DataFrame

前端 未结 3 1426
礼貌的吻别
礼貌的吻别 2020-12-08 10:19

If I have a dataframe that has columns that include the same name, is there a way to combine the columns that have the same name with some sort of function (i.e. sum)?

3条回答
  •  时光说笑
    2020-12-08 11:16

    I believe this does what you are after:

    df.groupby(lambda x:x, axis=1).sum()
    

    Alternatively, between 3% and 15% faster depending on the length of the df:

    df.groupby(df.columns, axis=1).sum()
    

    EDIT: To extend this beyond sums, use .agg() (short for .aggregate()):

    df.groupby(df.columns, axis=1).agg(numpy.max)
    

提交回复
热议问题