pandas中列名的异常排序

时间:2018-05-08 14:11:02

标签: python pandas

当我将数据框从pandas导出到Excel电子表格时,我的列顺序如下所示,其中“10”最大事件'被认为是“最大事件”之后的下一个事件,而不是“最大事件”#3;我希望它以数字顺序出现。即“最大的活动”,“最大的活动”,“#10; 10个最大的活动'

ID_1    Permit No.        ID_2       1 Largest Event    10 Largest Event    2 Largest Event
10220   To Be Permitted 0010001-24.1       4.0548                  0.822    3.9611

为什么会这样?这是一个小的格式错误,但它可能是非常令人瞩目的。

2 个答案:

答案 0 :(得分:5)

来自natsort reindex

from natsort import natsorted
l=['1 Largest Event','10 Largest Event','2 Largest Event']
natsorted(l)
Out[789]: ['1 Largest Event', '2 Largest Event', '10 Largest Event']
df=df.reindex(columns=natsorted(list(df)))

答案 1 :(得分:4)

问题是您的列按字典顺序排列为字符串。

因此需要通过首先将转换为int s的分割值进行排序:

df = df[sorted(df.columns, key=lambda x: int(x.split()[0]))]

<强>示例

cols = ['1 Largest Event', 
        '10 Largest Event', 
        '2 Largest Event',
        '3 Largest Event',
        '4 Largest Event',
        '5 Largest Event', 
        '6 Largest Event', 
        '7 Largest Event', 
        '8 Largest Event', 
        '9 Largest Event']

df = pd.DataFrame(0, columns=cols, index=[0])
print (df)
   1 Largest Event  10 Largest Event  2 Largest Event  3 Largest Event  \
0                0                 0                0                0   

   4 Largest Event  5 Largest Event  6 Largest Event  7 Largest Event  \
0                0                0                0                0   

   8 Largest Event  9 Largest Event  

df = df[sorted(df.columns, key=lambda x: int(x.split()[0]))]
print (df)

   1 Largest Event  2 Largest Event  3 Largest Event  4 Largest Event  \
0                0                0                0                0   

   5 Largest Event  6 Largest Event  7 Largest Event  8 Largest Event  \
0                0                0                0                0   

   9 Largest Event  10 Largest Event  
0                0                 0  

编辑:

您还可以过滤最后3列进行排序:

df = df[df.columns[:3].tolist() + sorted(df.columns[3:], key=lambda x: int(x.split()[0]))]
print (df)
    ID_1       Permit No.          ID_2  1 Largest Event  2 Largest Event  \
0  10220  To Be Permitted  0010001-24.1           4.0548           3.9611   

   10 Largest Event  
0             0.822