I have a pandas data frame and I want to sort column('Bytes') in Descending order and print highest 10 values and its related "Client IP" column value. Suppose following is a part of my dataframe. I have many different methods and failed?
0 Bytes Client Ip 0 1000 192.168.10.2 1 2000 192.168.10.12 2 500 192.168.10.4 3 159 192.168.10.56
Following prints only the raw which has the highest value.
print df['Bytes'].argmax()
To sort a dataframe based on the values of a column but in descending order so that the largest values of the column are at the top, we can use the argument ascending=False.
In order to sort the data frame in pandas, function sort_values() is used. Pandas sort_values() can sort the data frame in Ascending or Descending order.
I think you can use nlargest
(New in pandas
version 0.17.0
):
print df 0 Bytes Client Ip 0 1 1 1000 192.168.10.2 1 0 0 2000 192.168.10.12 2 2 2 500 192.168.10.4 3 3 3 159 192.168.10.56 print df.nlargest(3, 'Client') 0 Bytes Client Ip 1 0 0 2000 192.168.10.12 0 1 1 1000 192.168.10.2 2 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 Ip 1 1 2000 192.168.10.12 0 0 1000 192.168.10.2 2 2 500 192.168.10.4 3 3 159 192.168.10.56
To take the first 10 values use .head(10)
.
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