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:
Is there a way in pandas to sort these versions so that 0.2 is bigger than 0.1 but smaller than 0.10?
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
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
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