如何重新排序pandas中的某些行索引

时间:2017-04-25 05:51:46

标签: python-2.7 pandas

我有一个这样的数据框:

              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

作为一个例子。如果我想将第三行和第四行移动到第一行和第二行,我应该使用哪个命令?我将根据频率更改行的顺序,然后我可以做些什么来实现它?非常感谢你。

3 个答案:

答案 0 :(得分:3)

假设您的数据框名为df 使用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

答案 1 :(得分:2)

根据frequency使用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

对于排序列:

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

对两者进行排序 - indexcolumns

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 :(得分:0)

这很实用,因为你只有4列 假设dataFrame执行:

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

实际上是重写dataFrame,