3

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 forth row to be the first and the second row, which command shall I use? And I will change the order of the rows depending on the frequency, then what can I do to implement it? Thank you very much.

Dogod
  • 283
  • 5
  • 12
  • Possible duplication : https://stackoverflow.com/questions/13148429/how-to-change-the-order-of-dataframe-columns/39237712 – jkhadka Apr 25 '17 at 05:53
  • Do you mean columns or rows? Can you provide what your expected output looks like. At the moment, I'm confused. I've provided a way to move the rows. However, I'm very unclear as to what you mean by order the rows by frequency. You have frequency in you index (which is already sorted) and frequencies in you column headers. – piRSquared Apr 25 '17 at 06:04
  • Sorry for the confusion, what I want to is like move 9.003333e+07 this row from the second row to the first row and keep others the same. This is just an example, I may sort the frequency and move the frequency I want to the top rows. – Dogod Apr 26 '17 at 06:02

3 Answers3

3

Assuming you dataframe is named df
Use np.r_ to create the appropriate slice

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
piRSquared
  • 285,575
  • 57
  • 475
  • 624
  • Thank you for your help. But I do not what is the exact row index, instead, I know the frequency like 9010000.0 or 9003330.0, is there any any to do this? Thank you very much. – Dogod Apr 26 '17 at 06:32
2

For change order of index values by 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 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 sorts both - 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
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
0

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

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

which is actually rewriting the dataFrame,

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97