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'
?
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'
?
I think this will work, can you provide a specif example if not?
full['DEM'] = full.groupby('state').mean()['demVote']
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