Pandas: grouping and aggregation with multiple functions

前端 未结 4 1720
南笙
南笙 2021-01-14 04:52

Situation

I have a pandas dataframe defined as follows:

import pandas as pd

headers = [\'Group\', \'Element\', \'Case\', \'Score\', \'Evaluation\'         


        
4条回答
  •  伪装坚强ぢ
    2021-01-14 05:23

    My Take

    g = df.set_index('Group').groupby(level='Group', group_keys=False)
    
    result = g.apply(
        pd.DataFrame.nlargest, n=1, columns='Score'
    )
    
    def f(x):
        x = 'value' if x == 'Score' else x
        return 'Max_score_' + x.lower()
    
    result.drop('Evaluation', 1).rename(columns=f).assign(
        Min_evaluation=g.Evaluation.min().values).reset_index()
    
      Group  Max_score_element Max_score_case  Max_score_value  Min_evaluation
    0     A                  1              y             9.19            0.41
    1     B                  2              x             9.12            0.10
    

提交回复
热议问题