[PYTHON] Sort by specifying conditions in CASTable

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.

Get a table from the database

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

Sort

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

Specify ascending / descending order

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

Summary

Sorting data is a basic operation. Please specify the conditions in detail as an option while using sort_values.

SAS for Developers | SAS

Recommended Posts

Sort by specifying conditions in CASTable
Sort the elements of the array by specifying the conditions
Sort by date in python
[Python] Sort iterable by multiple conditions
When specifying multiple keys in python sort
Follow back by specifying conditions using tweepy
How to sort by specifying a column in the Python Numpy array.
Sort tuple list in Python by specifying the ascending / descending order of multiple keys
Bubble sort in Python
[Python] Get element by specifying name attribute in BeautifulSoup
Access Github by specifying the SSH key in GitPython
Custom sort in Python3
Differences in prices by prefecture (2019)
Sort by dict type value value
Naturally sort Path in Python
Sort by file modification date
Sort the file names obtained by Python glob in numerical order
[Python] Get elements by specifying attributes with prefix search in BeautifulSoup
Sort of tuple array can be accelerated by specifying key (Python)