【问题标题】:sort values with number and character in the same string A1,B1在同一字符串 A1,B1 中使用数字和字符对值进行排序
【发布时间】:2021-09-23 10:32:36
【问题描述】:

我有一个列值为 A1 A2 A3 B1 B2 B3 ....

我想排序为: A1 B1 A2 B2 A3 B3 ... 所以按数字排序,然后按字母排序。我该怎么做?

【问题讨论】:

  • 嗨,请不要使用 stackoverflow 来简单地获取您工作的答案。你能告诉我们你尝试了什么吗?

标签: python-3.x dataframe sorting


【解决方案1】:

试试这个:

column = 3
df[column] = sorted(list(df[column]), key=lambda x: int(x[1:])+ord(x[0]))

【讨论】:

  • 谢谢!它不工作。我需要对第 3 列中的值进行排序,总共有 5 列。
  • 抱歉,我以为您在尝试对列进行排序。我已经编辑了代码。
  • 我对你写的内容做了一些修改,现在可以了!
  • column = 'Image' df[column] = sorted(list(df[column]), key=lambda x: int(x[1:]) + ord(x[1]))
猜你喜欢
  • 1970-01-01
  • 2012-08-29
  • 2016-08-28
  • 2014-10-28
  • 2012-11-14
  • 2022-11-26
  • 2017-06-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多