1. 程式人生 > >Python中pandas的DataFrame按特定的list順序排序—reindex函式

Python中pandas的DataFrame按特定的list順序排序—reindex函式

如果一個DataFrame的index亂了,按正常的sort_index無法排序,則可以用reindex函式

import numpy as np
import pandas as pd
>>> randWeek=['Tues','Thur','Mon','Sat','Fri','Sun','Wed']
>>> frame=pd.DataFrame(np.random.randn(7,3),columns=list('abc'),index=randWeek)
>>> frame
                 a         b         c
    Tues  0.054086
-2.546081 0.051492 Thur 0.232368 -0.937215 0.883313 Mon -1.004652 1.266954 0.321374 Sat 2.003404 0.514108 0.640477 Fri -0.631527 0.939361 -0.013297 Sun 0.492959 -0.171945 0.030827 Wed -0.334522 -0.334029 0.575805 >>> normalWeek=['Mon','Tues','Wed','Thur','Fri','Sat','Sun'] >>>
frame.reindex(normalWeek) a b c Mon -1.004652 1.266954 0.321374 Tues 0.054086 -2.546081 0.051492 Wed -0.334522 -0.334029 0.575805 Thur 0.232368 -0.937215 0.883313 Fri -0.631527 0.939361 -0.013297 Sat 2.003404 0.514108 0.640477 Sun 0.492959 -0.171945 0.030827

指定的list中的index可以比DataFrame中已有的index多或少

>>> otherWeek1=['Tues','Wed','Thur','Fri','Sat','Sun']
>>> frame.reindex(otherWeek1)
                 a         b         c
    Tues  0.054086 -2.546081  0.051492
    Wed  -0.334522 -0.334029  0.575805
    Thur  0.232368 -0.937215  0.883313
    Fri  -0.631527  0.939361 -0.013297
    Sat   2.003404  0.514108  0.640477
    Sun   0.492959 -0.171945  0.030827

>>> otherWeek2=['Mon','Tues','Wed','Thur','Fri','Sat','Sun','lalala']
>>> frame.reindex(otherWeek2)
                   a         b         c
    Mon    -1.004652  1.266954  0.321374
    Tues    0.054086 -2.546081  0.051492
    Wed    -0.334522 -0.334029  0.575805
    Thur    0.232368 -0.937215  0.883313
    Fri    -0.631527  0.939361 -0.013297
    Sat     2.003404  0.514108  0.640477
    Sun     0.492959 -0.171945  0.030827
    lalala       NaN       NaN       NaN

人生苦短,我用python~