How to change the index order of a row in pandas

I have a dataframe like this:

              100MHz_Dif0  102MHz_Dif0  100MHz_Dif1  102MHz_Dif1
Frequency                                                       
9.000000e+07   -70.209000   -65.174004   -66.063004   -66.490997
9.003333e+07   -70.628998   -65.196999   -66.339996   -66.461998
9.006667e+07   -70.405998   -65.761002   -65.432999   -65.549004
9.010000e+07   -70.524002   -65.552002   -66.038002   -65.887001
9.013333e+07   -70.746002   -65.658997   -65.086998   -65.390999
9.016667e+07   -70.884003   -66.209999   -64.887001   -65.397003
9.020000e+07   -70.752998   -66.019997   -65.308998   -66.571999
9.023333e+07   -70.447998   -65.858002   -65.500000   -65.028999
9.026667e+07   -70.452003   -65.832001   -66.032997   -65.005997
9.030000e+07   -71.219002   -65.739998   -65.961998   -65.986000
9.033333e+07   -71.095001   -65.820999   -67.112999   -65.977997
9.036667e+07   -70.834000   -65.926003   -66.348000   -65.568001

      

as an example. If I want to move the third row and fourth row to the first and second rows, which command should I use? And I will change the order of the lines depending on the frequency, and what can I do to implement it? Thank you very much.

+3


source to share


3 answers


For the order in which the index values ​​change to, frequency

use sort_index

:

df = df.sort_index()
print (df)
            100MHz_Dif0  102MHz_Dif0  100MHz_Dif1  102MHz_Dif1
Frequency                                                     
90000000.0   -70.209000   -65.174004   -66.063004   -66.490997
90033330.0   -70.628998   -65.196999   -66.339996   -66.461998
90066670.0   -70.405998   -65.761002   -65.432999   -65.549004
90100000.0   -70.524002   -65.552002   -66.038002   -65.887001
90133330.0   -70.746002   -65.658997   -65.086998   -65.390999
90166670.0   -70.884003   -66.209999   -64.887001   -65.397003
90200000.0   -70.752998   -66.019997   -65.308998   -66.571999
90233330.0   -70.447998   -65.858002   -65.500000   -65.028999
90266670.0   -70.452003   -65.832001   -66.032997   -65.005997
90300000.0   -71.219002   -65.739998   -65.961998   -65.986000
90333330.0   -71.095001   -65.820999   -67.112999   -65.977997
90366670.0   -70.834000   -65.926003   -66.348000   -65.568001

      

And for the sort columns:



df = df.sort_index(axis=1)
print (df)
            100MHz_Dif0  100MHz_Dif1  102MHz_Dif0  102MHz_Dif1
Frequency                                                     
90000000.0   -70.209000   -66.063004   -65.174004   -66.490997
90033330.0   -70.628998   -66.339996   -65.196999   -66.461998
90066670.0   -70.405998   -65.432999   -65.761002   -65.549004
90100000.0   -70.524002   -66.038002   -65.552002   -65.887001
90133330.0   -70.746002   -65.086998   -65.658997   -65.390999
90166670.0   -70.884003   -64.887001   -66.209999   -65.397003
90200000.0   -70.752998   -65.308998   -66.019997   -66.571999
90233330.0   -70.447998   -65.500000   -65.858002   -65.028999
90266670.0   -70.452003   -66.032997   -65.832001   -65.005997
90300000.0   -71.219002   -65.961998   -65.739998   -65.986000
90333330.0   -71.095001   -67.112999   -65.820999   -65.977997
90366670.0   -70.834000   -66.348000   -65.926003   -65.568001

      

And for sorting - index

and columns

:

df = df.sort_index(axis=1).sort_index()
print (df)
            100MHz_Dif0  100MHz_Dif1  102MHz_Dif0  102MHz_Dif1
Frequency                                                     
90000000.0   -70.209000   -66.063004   -65.174004   -66.490997
90033330.0   -70.628998   -66.339996   -65.196999   -66.461998
90066670.0   -70.405998   -65.432999   -65.761002   -65.549004
90100000.0   -70.524002   -66.038002   -65.552002   -65.887001
90133330.0   -70.746002   -65.086998   -65.658997   -65.390999
90166670.0   -70.884003   -64.887001   -66.209999   -65.397003
90200000.0   -70.752998   -65.308998   -66.019997   -66.571999
90233330.0   -70.447998   -65.500000   -65.858002   -65.028999
90266670.0   -70.452003   -66.032997   -65.832001   -65.005997
90300000.0   -71.219002   -65.961998   -65.739998   -65.986000
90333330.0   -71.095001   -67.112999   -65.820999   -65.977997
90366670.0   -70.834000   -66.348000   -65.926003   -65.568001

      

+2


source


Assuming the dataframe is named df


Use to create a matching snippet np.r_



df.iloc[np.r_[[2, 3], [0, 1], 4:]]

                100MHz_Dif0  102MHz_Dif0  100MHz_Dif1  102MHz_Dif1
Frequency                                                     
90066670.0   -70.405998   -65.761002   -65.432999   -65.549004
90100000.0   -70.524002   -65.552002   -66.038002   -65.887001
90000000.0   -70.209000   -65.174004   -66.063004   -66.490997
90033330.0   -70.628998   -65.196999   -66.339996   -66.461998
90000000.0   -70.209000   -65.174004   -66.063004   -66.490997
90033330.0   -70.628998   -65.196999   -66.339996   -66.461998
90066670.0   -70.405998   -65.761002   -65.432999   -65.549004
90100000.0   -70.524002   -65.552002   -66.038002   -65.887001

      

+3


source


This can be practical since you only have 4 columns assuming the dataFrame is:

dataFrame = dataFrame[['100MHz_Dif1','102MHz_Dif1','100MHz_Dif0', '102MHz_Dif0']]

      

which actually rewrites the dataFrame,

0


source







All Articles