[英]Sorting columns in pandas dataframe
I have a dataframe with column headers "DIV3, DIV4, DIV5 ... DIV30" 我有一个带有列标题“DIV3,DIV4,DIV5 ... DIV30”的数据框
My problem is that pandas will sort the columns in the following way: 我的问题是pandas会按以下方式对列进行排序:
DIV10, DIV11, DIV12..., DIV3, DIV4, DIV5
Is there a way to arrange it such that the single digit numbers come first? 有没有办法安排它,以便单个数字首先出现? Ie:
即:
DIV3, DIV4, DIV5... DIV30
You can solve this by sorting in "human order" : 你可以通过“人类秩序”排序来解决这个问题:
import re
import pandas as pd
def natural_keys(text):
'''
alist.sort(key=natural_keys) sorts in human order
http://nedbatchelder.com/blog/200712/human_sorting.html
(See Toothy's implementation in the comments)
'''
def atoi(text):
return int(text) if text.isdigit() else text
return [atoi(c) for c in re.split('(\d+)', text)]
columns = ['DIV10', 'DIV11', 'DIV12', 'DIV3', 'DIV4', 'DIV5']
df = pd.DataFrame([[1]*len(columns)], columns=columns)
print(df)
# DIV10 DIV11 DIV12 DIV3 DIV4 DIV5
# 0 1 1 1 1 1 1
df = df.reindex(columns=sorted(df.columns, key=natural_keys))
print(df)
yields 产量
DIV3 DIV4 DIV5 DIV10 DIV11 DIV12
0 1 1 1 1 1 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.