1

I have a df,

code    y_m       day_1     day_2
13      201906    30        40
13      201906    10        20
14      201903    20        40
14      201903    20        30

I want to create a column

df['delta'] = df['day_2'] - df['day_1']

code    y_m       day_1     day_2    delta
13      201906    30        40       10 
13      201906    10        20       10
14      201903    20        40       20
14      201903    20        30       10

then groupby code and y_m, sum the delta and get the group size into another dataframe at the same time; but

df.groupby(['code', 'y_m']).size().reset_index(name = 'count')

only get the group size; so how to get aggregated delta as well into the same dataframe;

code    y_m       delta    count
13      201906    20        2
14      201903    30        2
daiyue
  • 7,196
  • 25
  • 82
  • 149

0 Answers0