SAS Viya is an AI platform. It is available through languages such as Python, Java and R. A table object called CASTable is used in SAS Viya (CAS stands for Cloud Analytic Services). This time, I will explain how to sort the data in CASTable.
First, connect to SAS Viya.
import swat
conn = swat.CAS('server-name.mycompany.com', 5570, 'username', 'password')
Then get the CASTable. This time, I will use CSV of IRIS data.
tbl = conn.loadtable('data/iris.csv', caslib='casuser').casTable
Sorting uses the sort_values
method.
tbl.sort_values(['sepal_length', 'sepal_width'])
Let's check the data in this state. The head
method gets from the first line.
sorttbl.head(10)
sepal_length | sepal_width | petal_length | petal_width | species | |
---|---|---|---|---|---|
0 | 4.3 | 3.0 | 1.1 | 0.1 | setosa |
1 | 4.4 | 2.9 | 1.4 | 0.2 | setosa |
2 | 4.4 | 3.0 | 1.3 | 0.2 | setosa |
3 | 4.4 | 3.2 | 1.3 | 0.2 | setosa |
4 | 4.5 | 2.3 | 1.3 | 0.3 | setosa |
5 | 4.6 | 3.1 | 1.5 | 0.2 | setosa |
6 | 4.6 | 3.2 | 1.4 | 0.2 | setosa |
7 | 4.6 | 3.4 | 1.4 | 0.3 | setosa |
8 | 4.6 | 3.6 | 1.0 | 0.2 | setosa |
9 | 4.7 | 3.2 | 1.6 | 0.2 | setosa |
The reverse order uses the tail
method.
sorttbl.tail(5)
sepal_length | sepal_width | petal_length | petal_width | species | |
---|---|---|---|---|---|
145 | 7.7 | 2.6 | 6.9 | 2.3 | virginica |
146 | 7.7 | 2.8 | 6.7 | 2.0 | virginica |
147 | 7.7 | 3.0 | 6.1 | 2.3 | virginica |
148 | 7.7 | 3.8 | 6.7 | 2.2 | virginica |
149 | 7.9 | 3.8 | 6.4 | 2.0 | virginica |
You can specify the order in detail with the ʻascending` option.
sorttbl = tbl.sort_values(['sepal_length', 'sepal_width'], ascending=[False, True])
If you check the data with this, the order of the data should be changed.
sepal_length | sepal_width | petal_length | petal_width | species | |
---|---|---|---|---|---|
0 | 7.9 | 3.8 | 6.4 | 2.0 | virginica |
1 | 7.7 | 2.6 | 6.9 | 2.3 | virginica |
2 | 7.7 | 2.8 | 6.7 | 2.0 | virginica |
3 | 7.7 | 3.0 | 6.1 | 2.3 | virginica |
4 | 7.7 | 3.8 | 6.7 | 2.2 | virginica |
5 | 7.6 | 3.0 | 6.6 | 2.1 | virginica |
6 | 7.4 | 2.8 | 6.1 | 1.9 | virginica |
7 | 7.3 | 2.9 | 6.3 | 1.8 | virginica |
8 | 7.2 | 3.0 | 5.8 | 1.6 | virginica |
9 | 7.2 | 3.2 | 6.0 | 1.8 | virginica |
Sorting data is a basic operation. Please specify the conditions in detail as an option while using sort_values.
Recommended Posts