[英]Python Dataframe - how to sort values by numeric & alphabet (ascending) and string length (descending)
I want to sort 1 column in the dataframe by following logic:我想通过以下逻辑对 dataframe 中的 1 列进行排序:
Example dataframe - using name
column to sort and eventually adding an 'Order' column too:示例 dataframe - 使用
name
列进行排序并最终添加“订单”列:
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,我在下面尝试了 sort_values(),
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.您可以使用 ASCII 表的最后一个元素将名称填充为相同的长度,以便 pandas 知道如何自动排序。
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对 dataframe 进行排序后,您可以删除该列
df = df.drop('sort_name', axis=1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.