I would like to apply a natural sort order to a column in a pandas DataFrame
. The columns that I would like to sort might contain duplicates. I have seen the related Naturally sorting Pandas DataFrame
question, however it was concerning sorting the index, not any column.
Example
df = pd.DataFrame({'a': ['a22', 'a20', 'a1', 'a10', 'a3', 'a1', 'a11'], 'b': ['b5', 'b2', 'b11', 'b22', 'b4', 'b1', 'b12']})
a b
0 a22 b5
1 a20 b2
2 a1 b11
3 a10 b22
4 a3 b4
5 a1 b1
6 a11 b12
Natural sort column a
:
a b
0 a1 b11
1 a1 b1
2 a3 b4
3 a10 b22
4 a11 b12
5 a20 b2
6 a22 b5
Natural sort column b
:
a b
0 a1 b1
1 a20 b2
2 a3 b4
3 a22 b5
4 a1 b11
5 a11 b12
6 a10 b22