This topic describes how to sort data in a PyODPS DataFrame by using the sort() method. Each call to the sort() method is compiled into a MaxCompute SQL statement that contains an ORDER BY clause.
Prerequisites
Before you start, complete the following steps:
-
Activate MaxCompute.
-
Activate DataWorks.
-
Create a workflow in DataWorks. This example uses a workspace in basic mode.
Procedure
-
Download the test dataset and import it into MaxCompute.
-
Download and decompress the Iris dataset. Rename the iris.data file to iris.csv.
-
Create a table named pyodps_iris and upload the iris.csv dataset. For more information, see Create a table and upload data.
Use the following statement to create the table.
CREATE TABLE if not exists pyodps_iris ( sepallength DOUBLE comment 'Sepal length (cm)', sepalwidth DOUBLE comment 'Sepal width (cm)', petallength DOUBLE comment 'Petal length (cm)', petalwidth DOUBLE comment 'Petal width (cm)', name STRING comment 'Species' );
-
Log in to the DataWorks console and select a region in the upper-left corner.
In the left-side navigation pane, click Workspaces.
-
In the Actions column, click .
-
On the Data Development page, right-click the created workflow and choose .
-
In the Create Node dialog box, enter a node name and click OK.
In the PyODPS node, enter the following code.
from odps.df import DataFrame iris = DataFrame(o.get_table('pyodps_iris')) # Sort the data by a single column. By default, data is sorted in ascending order. print(iris.sort('sepalwidth').head(5)) # The following examples show two ways to sort data in descending order. # Method 1: Set the ascending parameter to False. print(iris.sort('sepalwidth', ascending=False).head(5)) # Method 2: Add a hyphen (-) prefix to the column name. print(iris.sort(-iris.sepalwidth).head(5)) # Sort the data by multiple columns. print(iris.sort(['sepalwidth', 'petallength']).head(5)) # To use a different sort order for each column, pass a list of Booleans to the `ascending` parameter. # The length of the list must match the number of columns to sort. print(iris.sort(['sepalwidth', 'petallength'], ascending=[True, False]).head(5)) print(iris.sort(['sepalwidth', -iris.petallength]).head(5))Click Run.
View the results on the Log tab.

The following is the complete run log:
Sql compiled: CREATE TABLE tmp_pyodps_d1b06785_dc18_4288_ad34_de860de1be08 LIFECYCLE 1 AS SELECT * FROM WB_BestPractice_dev.`pyodps_iris` t1 ORDER BY sepalwidth LIMIT 10000 Instance ID: 20191010061554817gwml0lim sepallength sepalwidth petallength petalwidth name 0 5.0 2.0 3.5 1.0 Iris-versicolor 1 6.0 2.2 5.0 1.5 Iris-virginica 2 6.2 2.2 4.5 1.5 Iris-versicolor 3 6.0 2.2 4.0 1.0 Iris-versicolor 4 5.5 2.3 4.0 1.3 Iris-versicolor Sql compiled: CREATE TABLE tmp_pyodps_3cb90bb2_fb95_43fb_ae84_f2b5a27d72dc LIFECYCLE 1 AS SELECT * FROM WB_BestPractice_dev.`pyodps_iris` t1 ORDER BY sepalwidth DESC LIMIT 10000 Instance ID: 20191010061601287gs086792 sepallength sepalwidth petallength petalwidth name 0 5.7 4.4 1.5 0.4 Iris-setosa 1 5.5 4.2 1.4 0.2 Iris-setosa 2 5.2 4.1 1.5 0.1 Iris-setosa 3 5.8 4.0 1.2 0.2 Iris-setosa 4 5.4 3.9 1.3 0.4 Iris-setosa Sql compiled: CREATE TABLE tmp_pyodps_97b080bb_e014_48e8_a310_4b45fcd6a2ed LIFECYCLE 1 AS SELECT * FROM WB_BestPractice_dev.`pyodps_iris` t1 ORDER BY sepalwidth DESC LIMIT 10000 Instance ID: 20191010061606927g6emz192 sepallength sepalwidth petallength petalwidth name 0 5.7 4.4 1.5 0.4 Iris-setosa 1 5.5 4.2 1.4 0.2 Iris-setosa 2 5.2 4.1 1.5 0.1 Iris-setosa 3 5.8 4.0 1.2 0.2 Iris-setosa 4 5.4 3.9 1.3 0.4 Iris-setosa Sql compiled: CREATE TABLE tmp_pyodps_6fe37b6e_6705_4052_b733_211eb9bd16ac LIFECYCLE 1 AS SELECT * FROM WB_BestPractice_dev.`pyodps_iris` t1 ORDER BY sepalwidth, petallength LIMIT 10000 Instance ID: 20191010061611714gn586792 sepallength sepalwidth petallength petalwidth name 0 5.0 2.0 3.5 1.0 Iris-versicolor 1 6.0 2.2 4.0 1.0 Iris-versicolor 2 6.2 2.2 4.5 1.5 Iris-versicolor 3 6.0 2.2 5.0 1.5 Iris-virginica 4 4.5 2.3 1.3 0.3 Iris-setosa Sql compiled: CREATE TABLE tmp_pyodps_a52c805c_94a1_4a75_a6af_4fc9ed06ae68 LIFECYCLE 1 AS SELECT * FROM WB_BestPractice_dev.`pyodps_iris` t1 ORDER BY sepalwidth, petallength DESC LIMIT 10000 Instance ID: 20191010061616553gw3m9592 sepallength sepalwidth petallength petalwidth name 0 5.0 2.0 3.5 1.0 Iris-versicolor 1 6.0 2.2 5.0 1.5 Iris-virginica 2 6.2 2.2 4.5 1.5 Iris-versicolor 3 6.0 2.2 4.0 1.0 Iris-versicolor 4 6.3 2.3 4.4 1.3 Iris-versicolor Sql compiled: CREATE TABLE tmp_pyodps_aac5538e_9b40_4078_b3c6_852b99c663c1 LIFECYCLE 1 AS SELECT * FROM WB_BestPractice_dev.`pyodps_iris` t1 ORDER BY sepalwidth, petallength DESC LIMIT 10000 Instance ID: 20191010061621329gvmkc292 sepallength sepalwidth petallength petalwidth name 0 5.0 2.0 3.5 1.0 Iris-versicolor 1 6.0 2.2 5.0 1.5 Iris-virginica 2 6.2 2.2 4.5 1.5 Iris-versicolor 3 6.0 2.2 4.0 1.0 Iris-versicolor 4 6.3 2.3 4.4 1.3 Iris-versicolor