I have a Pandas DataFrame like this:
Browsers Sessions
Chrome 201
IE 136
Safari 101
Firefox 36
SamsungBrowse 12
Opera 6
and what I need is display top 3 values and sum the rest as 'other':
Browsers Sessions
Chrome 201
IE 136
Safari 101
Other 54
Any ideas how this could be done?
Try this:
In [39]: result = df.nlargest(3, columns='Sessions')
In [40]: result.loc[len(result)] = ['Others', df.loc[~df.Browsers.isin(result.Browsers), 'Sessions'].sum()]
In [41]: result
Out[41]:
Browsers Sessions
0 Chrome 201
1 IE 136
2 Safari 101
3 Others 54
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With