With Pandas in Python, select the highest value row for each group

后端 未结 2 669
礼貌的吻别
礼貌的吻别 2021-01-25 05:29

With Pandas, for the following data set

author1,category1,10.00
author1,category2,15.00
author1,category3,12.00
author2,category1,5.00
author2,category2,6.00
aut         


        
2条回答
  •  再見小時候
    2021-01-25 06:14

    import pandas as pd
    
    df = pd.read_csv("in.csv", names=("Author","Cat","Val"))
    
    print(df.groupby(['Author'])['Val'].max())
    

    To get the df:

    inds = df.groupby(['Author'])['Val'].transform(max) == df['Val']
    df = df[inds]
    df.reset_index(drop=True, inplace=True)
    print(df)
        Author        Cat  Val
    0  author1  category2   15
    1  author2  category4    9
    2  author3  category1    7
    3  author3  category3    7
    

提交回复
热议问题