简体   繁体   English

Python Dataframe - 如何按数字和字母(升序)和字符串长度(降序)对值进行排序

[英]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 列进行排序:

  1. Numeric goes first - by ascending order;数字优先 - 按升序排列;
  2. Alphabet follows - by ascending order;字母跟随 - 按升序排列;
  3. Lastly, string length - by descending order.最后,字符串长度 - 按降序排列。

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.

相关问题 如何先按数字降序排序,然后按字母升序排序 - How to sort a list by number first in descending order and then by alphabet in ascending order 如何按数字降序和字母升序对我的列表进行排序? - How to sort my list by descending numbers and ascending alphabet? 在Python字典中按值(降序)和键(升序)排序 - Sort by values (descending) and then keys (ascending) in Python dictionary Python,如何按字符串列升序对数据框进行排序 - Python, how to sort dataframe by string column in ascending 如何在 Python 3 中按升序对二维数组列和按降序对行进行排序 - How to sort 2D array column by ascending and row by descending in Python 3 如何在python中按升序排序前半部分,按降序排序后半部分? - How to sort first half in ascending and second half in descending order in python? 如何在Python中按升序对第一个变量,按自定义顺序对第二个变量以及对降序中的第三个变量对DataFrame进行排序 - How can I sort DataFrame in Python on 1st variable in ascending, on 2nd variable in custom order and on 3rd variable in Descending 冒泡排序帮助python - 升序和降序 - Bubble Sort help in python - ascending & descending order Python:按升序和降序合并排序 - Python: Merge Sort in ascending and descending order 如何按升序或降序对 Seaborn 图形进行排序 - How to Sort Seaborn Graph in ascending or descending order
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM