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

前端 未结 2 1665
梦谈多话
梦谈多话 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
    
    0 讨论(0)
  • 2021-01-22 16:40

    You could do it without groupby and use drop_duplicate and join:

    df.join(df.drop_duplicates('order_id')\
      .eval('crow = distance_theo / bird_distance')[['crow']]).ffill()
    

    or use assign instead of eval per @jezraela comments below:

    df1.join(df1.drop_duplicates('order_id')\
       .assign(crow=df1.distance_theo / df1.bird_distance)[['crow']]).ffill()
    

    Output:

       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
    
    0 讨论(0)
提交回复
热议问题