0
 col = full.groupby('state')[['demVote']].mean()

full is the name of the original table. How would I append the results of this group by statement on to the original table full as a column called 'DEM'?

sacuL
  • 49,704
  • 8
  • 81
  • 106
user9347880
  • 123
  • 1
  • 2
  • 5

2 Answers2

0

I think this will work, can you provide a specif example if not?

full['DEM'] = full.groupby('state').mean()['demVote']
Pedro
  • 355
  • 4
  • 18
0

use transform:

full['DEM'] = full.groupby('state')['demVote'].transform('mean')

For example:

>>> full
   demVote state
0        1    ca
1        2    ca
2        3    ny
3        4    ny

full['DEM'] = full.groupby('state')['demVote'].transform('mean')

>>> full
   demVote state  DEM
0        1    ca  1.5
1        2    ca  1.5
2        3    ny  3.5
3        4    ny  3.5
sacuL
  • 49,704
  • 8
  • 81
  • 106