您可以将行转换为二维数组,按第二列排序并将其重新整形为单行。
#This is your dataFrame:
columns = ["x_0", "x_1", "y_0", "y_1", "z_0", "z_1"]
data = np.array([5,6,4,2,6,1]).reshape(-1,6))
df = pd.DataFrame(data=data,columns=columns)
#Output
x_0 x_1 y_0 y_1 z_0 z_1
0 6 1 4 2 5 6
#Select your row, in this case row=0; get the values as a numpy array and reshape it to a 2D array
row=0
M = df.iloc[row].values
M = M.reshape(-1,2)
#Output
array([[6, 1],
[4, 2],
[5, 6]])
#Create a temporary dataframe that you can use to sort by second column (i.e. column 1)
df_temp = pd.DataFrame(M).sort_values(by=1)
#Output
0 1
0 6 1
1 4 2
2 5 6
#Get the sorted values back to a numpy 2D array and reshape it;
df_temp.values.reshape(-1,6)
#Output
array([[6, 1, 4, 2, 5, 6]])
#And here you are
df.iloc[0] = df_temp.values.reshape(-1,6)
#Output
x_0 x_1 y_0 y_1 z_0 z_1
0 6 1 4 2 5 6
你可以把它们放在一行中:
df.iloc[0] = pd.DataFrame(df.iloc[0].values.reshape(-1,2)).sort_values(by=1).values.reshape(-1,6)