-1

So my issue is I want to get the value of the subtraction of the secondary groups for each primary group.

I applied groupby() for two columns 'Country' and 'doc_type' and I want the subtraction of the values (SAL-SRT) obtained for each Country, using pythons pandas

Country      doc_type
Afghanistan  SAL         4375.0
             SRT          144.0
Algeria      SAL         8613.0
             SRT           28.0
Australia    SAL          356.0
Henry Ecker
  • 34,399
  • 18
  • 41
  • 57

1 Answers1

1

try pivot()

df.pivot(index='Country', columns='doc_type', values='values')

reset_index() & fillna(0), then subtract:

df['delta'] = df.SAL - df.SRT
MAFiA303
  • 1,157
  • 11
  • 10