对Pandas数据帧进行排序并打印最高n值

Nil*_*age 27 sorting max dataframe pandas

我有一个pandas数据框,我想按降序排序列('字节')并打印最高10个值及其相关的"客户端IP"列值.假设以下是我的数据帧的一部分.我有很多不同的方法而且失败了?

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 
Run Code Online (Sandbox Code Playgroud)

以下仅打印具有最高值的原始值.

print df['Bytes'].argmax()
Run Code Online (Sandbox Code Playgroud)

jez*_*ael 31

我想你可以使用nlargest(新pandas版本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
Run Code Online (Sandbox Code Playgroud)


And*_*den 22

注意: sort已弃用 - 请sort_values改用

sort降的使用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
Run Code Online (Sandbox Code Playgroud)

使用前10个值.head(10).