I want to sort 1 column in the dataframe by following logic:
Example dataframe - using name
column to sort and eventually adding an 'Order' column too:
import pandas as pd
df_1 = pd.DataFrame({'name': ['3D', '3DD', 'AC', 'AC-', 'BE', '2C','BED'], 'score': [2, 4, 2, 3, 10, 8, 2]})
I have tried sort_values() per below,
df_1['Len'] = df_1['name'].apply(lambda x: len(x))
df_1.sort_values(by=['name', 'Len'], ascending=[True, False], inplace=True,ignore_index=True)
df_1.drop(columns=['Len'], inplace=True)
df_1['Order'] = df_1.index+1
however, giving me this result - basically the string length by descending sorting didn't work:
name score Order
0 2C 8 1
1 3D 2 2
2 3DD 4 3
3 AC 2 4
4 AC- 3 5
5 BE 10 6
6 BED 2 7
Based on my above sorting logics, this is the desired results:
name score Order
0 2C 8 1
1 3DD 4 2
2 3D 2 3
3 AC- 3 4
4 AC 2 5
5 BED 2 6
6 BE 10 7
Thank you!
You can fill the names to have the same length using the last element of the ASCII table so pandas will know how to sort automatically.
name
0 2C
1 3D
2 3DD
3 AC
4 AC-
5 BE
6 BED
max_length = max(df.name.str.len())
df.loc['sort_name']=df.name.str.pad(max_length,'right','~')
df.sort_values('sort_name', inplace=True, ignore_index=True)
name sort_name
0 2C 2C~
2 3DD 3DD
1 3D 3D~
4 AC- AC-
3 AC AC~
6 BED BED
5 BE BE~
This will take the maximum length of the column as the number to pad.
After you have sorted the dataframe you can delete the column with
df = df.drop('sort_name', axis=1)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.