I wanted to sort one column first by integer then combination of integer and string then string.
Below is my data frame.
import pandas as pd
df = pd.DataFrame({'name': ['aaa', 'ccc', 'bbb', '1c2z45zz', 55, 3, 'et45']})
Below is my expected output.
df = pd.DataFrame({'name': [3, 55, '1c2z45zz', 'et45', 'aaa', 'bbb', 'ccc']})
I have tried many method but all gives first combination of string and integer then integer then string.
import natsort
df.iloc[natsort.index_humansorted(df.name)]
then I tried below one.
max_length = max(df.name.str.len())
df['sort_name']=df.name.str.pad(max_length,'right','~')
df.sort_values('sort_name', inplace=True, ignore_index=True)
and the last I tried.
df_sorted = df.astype(str).sort_values(by='A',key=lambda x: (x.isdigit(), x))
You can use
numpy.lexsortwith your different conditions:Output:
Other example: