Pandas: groupby and make a new column applying aggregate to two columns

前端 未结 2 1666
梦谈多话
梦谈多话 2021-01-22 15:41

I\'m having a difficulty with applying agg to a groupby pandas dataframe.

I have a dataframe df like this:

order_i         


        
2条回答
  •  情话喂你
    2021-01-22 16:18

    Using groupby with first:

    s = df.groupby('order_id').transform('first')
    df.assign(crow=s.distance_theo.div(s.bird_distance))
    
       order_id  distance_theo  bird_distance      crow
    0        10            100             80  1.250000
    1        10             80             80  1.250000
    2        10             70             80  1.250000
    3        11             90             70  1.285714
    4        11             70             70  1.285714
    5        11             60             70  1.285714
    6        12            200            180  1.111111
    7        12            150            180  1.111111
    8        12            100            180  1.111111
    9        12             60            180  1.111111
    

提交回复
热议问题