I come across this problem too, after googling a lot (the first page I find is this SO question :D), I suppose my solution is worth to mention.
So for now there is two sort functions in pandas, sort_values
and sort_index
, neither of them have a key
parameter for us to pass a custom sort function to it. See this github issue.
jezrael's answer is very helpful and I'll build my solution based on that.
df['ver'] = sorted(df['ver'], key=StrictVersion)
is useful only if the verion column is the single column in the DataFrame, otherwise we need to sort the other columns following the version column.
jezrael reindex
the DataFrame, because the wanted index order can be obtained by the buitin sorted
function, who does have a key
parameter.
But, what if the version is not the index and I don't want to set_index('ver')
?
We can use apply
to map the original version string to a StrictVersion
object, then sort_values
will sort in the wanted order:
from distutils.version import StrictVersion
df['ver'] = df['ver'].apply(StrictVersion)
df.sort_values(by='ver')