数值排序包含数字和字符串的列(pandas/python) [英] numerical sort a column containing numbers and strings (pandas/python)

查看:482
本文介绍了数值排序包含数字和字符串的列(pandas/python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须对第1列和第2列的数据帧进行排序;第1列包含数字和文本,应首先对其进行数字排序.在excel中,这是标准的排序方式,但在熊猫中却不是..在熊猫手册中,我找不到更多有关如何执行此操作的信息.

I have to sort a data frame on column 1 and 2; column 1 contains numbers and text, which should first be numerically sorted. In excel this is the standard way to sort, but not in pandas.. I couldn't find much info on how to do this in the pandas manual..

因此,此数据框:

Z   762320  296 1
Z   861349  297 0
1   865545  20  20
1   865584  297 0
22  865625  297 0
2   865628  292 5
10  865662  297 0
1   865665  296 0
11  865694  293 1
1   865700  297 0
10  866429  297 0
11  866438  297 0

应为:

1   865545  20  20
1   865584  297 0
1   865665  296 0
1   865700  297 0
2   865628  292 5
10  865662  297 0
10  866429  297 0
11  865694  293 1
11  866438  297 0
22  865625  297 0
Z   762320  296 1
Z   861349  297 0

当我执行df.sort([0,1])时,我得到:

when i do df.sort([0,1]) i get:

     0       1    2   3
1    1  865545   20  20
2    1  865584  297   0
3    1  865665  296   0
4    1  865700  297   0
6   10  865662  297   0
7   10  866429  297   0
8   11  865694  293   1
9   11  866438  297   0
5    2  865628  292   5
10  22  865625  297   0
0    Z  762320  296   1
11   Z  861349  297   0

推荐答案

您是指第0列和第1列吗?

Do you mean column 0 and 1?

>>> df.sort([0, 1])
     0       1    2   3
2    1  865545   20  20
3    1  865584  297   0
7    1  865665  296   0
9    1  865700  297   0
5    2  865628  292   5
6   10  865662  297   0
10  10  866429  297   0
8   11  865694  293   1
11  11  866438  297   0
4   22  865625  297   0 
0    Z  762320  296   1
1    Z  861349  297   0

[更新]

如果您的数据不是数字(所有元素都是字符串),则会发生这种情况.

This happens if your data is not numeric (all elements are strings).

>>> df.values
array([['Z', '762320', '296', '1'],
       ['Z', '861349', '297', '0'],
       ['1', '865545', '20', '20'],
       ['1', '865584', '297', '0'],
       ['22', '865625', '297', '0'],
       ['2', '865628', '292', '5'],
       ['10', '865662', '297', '0'],
       ['1', '865665', '296', '0'],
       ['11', '865694', '293', '1'],
       ['1', '865700', '297', '0'],
       ['10', '866429', '297', '0'],
       ['11', '866438', '297', '0']], dtype=object)

字符串排序是预期结果:

String ordering is the expected result:

>>> df.sort([0, 1])    
     0       1    2   3
2    1  865545   20  20
3    1  865584  297   0
7    1  865665  296   0
9    1  865700  297   0
6   10  865662  297   0
10  10  866429  297   0
8   11  865694  293   1
11  11  866438  297   0
5    2  865628  292   5
4   22  865625  297   0
0    Z  762320  296   1
1    Z  861349  297   0

尝试首先转换值:

>>> def convert(v):
...:    try:
...:        return int(v)    
...:    except ValueError:
...:        return v

>>> pandas.DataFrame([convert(c) for c in l] for l in df.values)\
      .sort([0, 1])

     0       1    2   3
2    1  865545   20  20
3    1  865584  297   0
7    1  865665  296   0
9    1  865700  297   0
5    2  865628  292   5
6   10  865662  297   0
10  10  866429  297   0
8   11  865694  293   1
11  11  866438  297   0
4   22  865625  297   0
0    Z  762320  296   1
1    Z  861349  297   0

有什么区别?元素现在是数字了:

What is the difference? The elements are numeric now:

>>> pandas.DataFrame([convert(c) for c in l] for l in df.values)\
      .sort([0, 1]).values

array([[1.0, 865545.0, 20.0, 20.0],
      [1.0, 865584.0, 297.0, 0.0],
      [1.0, 865665.0, 296.0, 0.0],
      [1.0, 865700.0, 297.0, 0.0],
      [2.0, 865628.0, 292.0, 5.0],
      [10.0, 865662.0, 297.0, 0.0],
      [10.0, 866429.0, 297.0, 0.0],
      [11.0, 865694.0, 293.0, 1.0],
      [11.0, 866438.0, 297.0, 0.0],
      [22.0, 865625.0, 297.0, 0.0],
      ['Z', 762320.0, 296.0, 1.0],
      ['Z', 861349.0, 297.0, 0.0]], dtype=object)

这篇关于数值排序包含数字和字符串的列(pandas/python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆