Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
684 views
in Technique[技术] by (71.8m points)

sorting - numerical sort a column containing numbers and strings (pandas/python)

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..

So this dataframe:

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

should be:

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

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

[update]

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

Try to convert the values first:

>>> 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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...