groupby: add percentages

User 2715 | 12/2/2015, 1:26:59 PM

with

finaleffectcount = cdssdata.groupby(keycolumns='FinalEffect',operations={'count': agg.COUNT()})

I get a lovely table with the 'Final Effect' and the counts. I'd like to add a column with the percentages as well, like 'percentage': agg.COUNT()/len(cdss_data) *100 but (obviously) that doesn't work.

Could someone help me out?

Comments

User 954 | 12/2/2015, 7:37:07 PM

Hello,

It could be easily achieved like that: import graphlab as gl sf = gl.SFrame({'a':[1,2,2,4,4],'b':[1,2,3,4,5]}) grouped_sf = sf.groupby('a',{'COUNT':gl.aggregate.COUNT()}) grouped_sf['percentage'] = grouped_sf['COUNT']/len(sf) * 100 print grouped_sf +---+-------+------------+ | a | COUNT | percentage | +---+-------+------------+ | 2 | 2 | 40.0 | | 4 | 2 | 40.0 | | 1 | 1 | 20.0 | +---+-------+------------+ [3 rows x 3 columns] I suggest you to look at our how-to and userguide. Especially the "SFrame" section.

https://dato.com/learn/how-to/ https://dato.com/learn/userguide/

Regards