Pandas: Calculate Median of Group over Columns

前端 未结 1 1140
鱼传尺愫
鱼传尺愫 2021-01-07 14:01

Given the following data frame:

import pandas as pd

df = pd.DataFrame({\'COL1\': [\'A\', \'A\',\'A\',\'A\',\'B\',\'B\'], 
                   \'COL2\' : [\'A         


        
相关标签:
1条回答
  • 2021-01-07 14:51

    You already had the idea -- groupby COL1 and COL2 and calculate median.

    m = df.groupby(['COL1', 'COL2'])[['COL3','COL4']].apply(np.median)
    m.name = 'MEDIAN'
    
    print df.join(m, on=['COL1', 'COL2'])
    
      COL1 COL2  COL3  COL4  MEDIAN
    0    A   AA     2     0     1.5
    1    A   AA     3     1     1.5
    2    A   BB     4     2     3.5
    3    A   BB     5     3     3.5
    4    B   BB     4     4     3.0
    5    B   BB     2     2     3.0
    
    0 讨论(0)
提交回复
热议问题