英文:
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 string
s 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论