Sort Pandas dataframe and print highest n values Sort Pandas dataframe and print highest n values pandas pandas

Sort Pandas dataframe and print highest n values


I think you can use nlargest (New in pandas version 0.17.0):

print df   0  Bytes  Client             Ip0  1      1    1000   192.168.10.21  0      0    2000  192.168.10.122  2      2     500   192.168.10.43  3      3     159  192.168.10.56print df.nlargest(3, 'Client')   0  Bytes  Client             Ip1  0      0    2000  192.168.10.120  1      1    1000   192.168.10.22  2      2     500   192.168.10.4


Note: sort is deprecated - use sort_values instead

To sort descending use ascending=False:

In [6]: df.sort('Bytes', ascending=False)Out[6]:   0  Bytes      Client Ip1  1   2000  192.168.10.120  0   1000   192.168.10.22  2    500   192.168.10.43  3    159  192.168.10.56

To take the first 10 values use .head(10).


df['Bytes'] = df['Bytes'].astype('int')print df.sort('Bytes', ascending=False).head(10)[['Bytes', 'Client-IP']]

I could solve it using above code with the help of Andy Hayden. :D