[PYTHON] How to use loc / iloc / ix to get by specifying a column 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 get data by specifying columns using loc / iloc / ix 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

Specify a column

Use loc to specify the column and specify the column name in the second argument.

tbl.loc[:, 'petal_width'].head()

Then you can only get the value of the specified column.

0    2.0
1    2.3
2    2.0
3    2.3
4    2.2
Name: petal_width, dtype: float64

This can be specified in multiple columns.

tbl.loc[:, 'sepal_length':'petal_length'].head()

For multiple columns, the header row is displayed. Since it is specified by the range, the sepal_width that exists between them is also displayed.

sepal_length sepal_width petal_length
0 7.9 3.8 6.4
1 7.7 2.6 6.9
2 7.7 2.8 6.7
3 7.7 3.0 6.1
4 7.7 3.8 6.7

If you want to specify the column, specify it as an array.

tbl.loc[:, ['petal_width', 'sepal_width']].head()
petal_width sepal_width
0 2.0 3.8
1 2.3 2.6
2 2.0 2.8
3 2.3 3.0
4 2.2 3.8

You can specify numbers instead of column names.

tbl.loc[:, 3].head()
0    2.0
1    2.3
2    2.0
3    2.3
4    2.2
Name: petal_width, dtype: float64

The same applies to multiple column specifications.

tbl.iloc[:, 0:3].head()
sepal_length sepal_width petal_length
0 7.9 3.8 6.4
1 7.7 2.6 6.9
2 7.7 2.8 6.7
3 7.7 3.0 6.1
4 7.7 3.8 6.7

When specifying a column, the same is true for an array.

tbl.iloc[:, [3, 1]].head()
petal_width sepal_width
0 2.0 3.8
1 2.3 2.6
2 2.0 2.8
3 2.3 3.0
4 2.2 3.8

Combinations of numbers and column names are also possible.

tbl.ix[:, [3, 'sepal_width']].head()
petal_width sepal_width
0 2.0 3.8
1 2.3 2.6
2 2.0 2.8
3 2.3 3.0
4 2.2 3.8

Summary

There are many situations where you want to analyze only some data. In such a case, use loc, iloc, or ix to quickly extract the data. Numbers are also easy to use for looping, so they should also be useful for automating analytical and computational processing.

SAS for Developers | SAS

Recommended Posts

How to use loc / iloc / ix to get by specifying a column in CASTable
How to sort by specifying a column in the Python Numpy array.
How to use calculated columns in CASTable
How to get a stacktrace in python
[Django] How to get data by specifying SQL.
A memorandum on how to use keras.preprocessing.image in Keras
How to use the __call__ method in a Python class
I tried "How to get a method decorated in Python"
How to get the last (last) value in a list in Python
How to get a list of built-in exceptions in python
How to get a quadratic array of squares in a spiral!
How to search by string to use mysql json_contains in SQLAlchemy
How to use classes in Theano
How to use SQLite in Python
How to use Mysql in python
How to use ChemSpider in Python
How to use PubChem in Python
Sort by specifying conditions in CASTable
How to display a specified column of files in Linux (awk)
How to get all the possible values in a regular expression
How to use Visual Recognition to get LINE ID from a girl
[Introduction to Python] How to use the in operator in a for statement?
How to get the vertex coordinates of a feature in ArcPy
[Python] How to use the for statement. A method of extracting by specifying a range or conditions.
[Introduction to Python] How to use class in Python?
How to get a value from a parameter store in lambda (using python)
How to use Anaconda interpreter in PyCharm
How to find a specific type (str, float etc) column in a DataFrame column
How to use __slots__ in Python class
How to use regular expressions in Python
How to use Map in Android ViewPager
How to use is and == in Python
How to get the "name" of a field whose value is limited by the choice attribute in Django's model
How to get a list of files in the same directory with python
How to use the C library in Python
A simple example of how to use ArgumentParser
How to clear tuples in a list (Python)
How to use Python Image Library in python3 series
How to get results from id in Celery
How to create a JSON file in Python
How to get help in an interactive shell
Summary of how to use MNIST in Python
How to implement a gradient picker in Houdini
How to notify a Discord channel in Python
How to get the files in the [Python] folder
How to use tkinter with python in pyenv
[Python] How to draw a histogram in Matplotlib
How to create a Rest Api in Django
Use pygogo to get the log in json.
How to read a file in a different directory
How to Mock a Public function in Pytest
Use os.getenv to get environment variables in Python
How to execute a schedule by specifying the Python time zone and execution frequency
How to use a library that is not originally included in Google App Engine
I want to use a python data source in Re: Dash to get query results
How to use fixture in Django to populate sample data associated with a user model
How to use any or all to check if it is in a dictionary (Hash)
I want to use a network defined by myself in PPO2 of Stable Baselines
How to use Docker to containerize your application and how to use Docker Compose to run your application in a development environment
How to use python multiprocessing (continued 3) apply_async in class with Pool as a member
How to specify a schema in Django's database settings