Pandas: apply different functions to different columns

前端 未结 3 727
逝去的感伤
逝去的感伤 2020-12-16 19:49

When using df.mean() I get a result where the mean for each column is given. Now let\'s say I want the mean of the first column, and the sum of the second. Is t

3条回答
  •  遥遥无期
    2020-12-16 20:10

    I think you can use the agg method with a dictionary as the argument. For example:

    df = pd.DataFrame({'A': [0, 1, 2], 'B': [3, 4, 5]})
    
    df =
    A   B
    0   0   3
    1   1   4
    2   2   5
    
    df.agg({'A': 'mean', 'B': sum})
    
    A     1.0
    B    12.0
    dtype: float64
    

提交回复
热议问题