有没有一种方法可以在数字内部对数字进行排序?

huangapple go评论53阅读模式
英文:

Is there a way to sort number within number

问题

import pandas as pd

file = 'read.xlsx'
df1 = pd.read_excel(file)
df1.sort_values(by='column_name', inplace=True)
print(df1)
英文:

Excel file:

5432
321
9870

import pandas as pd


file = 'read.xlsx'
df1 = pd.read_excel(file)
print(df1)

I want to sort the number within the number into

2345
1234
0789

答案1

得分: 1

使用列表推导将值转换为字符串并进行排序:

# 读取没有标题的Excel文件,所以列索引为0
df1 = pd.read_excel(file, header=None)

df1['new'] = [''.join(sorted(str(x))) for x in df1[0]]
print(df1)

输出结果:

     0    new
0  2345  2345
1  1234  1234
2  0789  0789
英文:

Use list comprehension with convert values to strings with sorted:

#read excel file without header, so column is 0
df1 = pd.read_excel(file, header=None)

df1['new'] = [''.join(sorted(str(x))) for x in df1[0]]
print (df1)
    col
0  2345
1  1234
2  0789

huangapple
  • 本文由 发表于 2023年2月27日 15:47:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75577903.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定