Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Pandas - how to get top n values and the sum of all other values

Tags:

python

pandas

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?

like image 620
W.Sun Avatar asked Dec 26 '16 09:12

W.Sun


1 Answers

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
like image 167
MaxU - stop WAR against UA Avatar answered Oct 19 '22 22:10

MaxU - stop WAR against UA