Pandas:groupby 并创建一个新列,将聚合应用到两列

Mak*_*aki 2 python group-by aggregate pandas

agg我在应用pandas 数据框时遇到困难groupby

我有一个df像这样的数据框:

order_id    distance_theo    bird_distance 
      10              100               80
      10               80               80
      10               70               80
      11               90               70
      11               70               70
      11               60               70
      12              200              180
      12              150              180
      12              100              180
      12               60              180
Run Code Online (Sandbox Code Playgroud)

我想要 groupby ,并通过将每个组中的第一行除以每个组的第一行(或任何行,因为一个组中只有一个值)order_id来创建一个新列。crowdistance_theobird_distancebird_distance

order_id    distance_theo    bird_distance    crow
      10              100               80    1.25
      10               80               80    1.25
      10               70               80    1.25
      11               90               70    1.29
      11               70               70    1.29
      11               60               70    1.29
      12              200              180    1.11
      12              150              180    1.11
      12              100              180    1.11
      12               60              180    1.11
Run Code Online (Sandbox Code Playgroud)

我的尝试: df.groupby('order_id').agg({'crow', lambda x: x.distance_theo.head(1) / x.bird_distance.head(1)})

但我收到一个错误:

'Series' object has no attribute 'distance_theo'
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?感谢您的任何建议!

use*_*203 5

groupby与以下一起使用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
Run Code Online (Sandbox Code Playgroud)