Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i sort semantic versions in pandas?

Tags:

python

pandas

I have a list of software releases as versions. The software follows the semantic version specification, meaning there is a major version, a minor version and patch versions:

  • 0.1
  • 0.2
  • 0.2.1
  • 0.3
  • ...
  • 0.10
  • 0.10.1

Is there a way in pandas to sort these versions so that 0.2 is bigger than 0.1 but smaller than 0.10?

like image 900
Muri Nicanor Avatar asked Dec 07 '22 17:12

Muri Nicanor


2 Answers

Pandas solution with sorted, StrictVersion solution and assign to column:

print (df)
      ver
0     0.1
1     0.2
2    0.10
3   0.2.1
4     0.3
5  0.10.1

from distutils.version import StrictVersion

df['ver'] = sorted(df['ver'], key=StrictVersion)
print (df)
      ver
0     0.1
1     0.2
2   0.2.1
3     0.3
4    0.10
5  0.10.1

EDIT:

For sort index is possible use reindex:

print (df)
        a  b
ver         
0.1     1  q
0.2     2  w
0.10    3  e
0.2.1   4  r
0.3     5  t
0.10.1  6  y

from distutils.version import StrictVersion

df = df.reindex(index=pd.Index(sorted(df.index, key=StrictVersion)))
print (df)
        a  b
0.1     1  q
0.2     2  w
0.2.1   4  r
0.3     5  t
0.10    3  e
0.10.1  6  y
like image 121
jezrael Avatar answered Jan 05 '23 11:01

jezrael


You can use the standard distutils for this!

from distutils.version import StrictVersion
versions = ['0.1', '0.10', '0.2.1', '0.2', '0.10.1']
versions.sort(key=StrictVersion)

Now it's sorted like this: ['0.1', '0.2', '0.2.1', '0.10', '0.10.1']

Source

like image 25
jarcobi889 Avatar answered Jan 05 '23 12:01

jarcobi889