LoginSignup
0
0

More than 3 years have passed since last update.

CASTableで一部のカラムだけ表示、分析する

Posted at

SAS ViyaはAIプラットフォームです。PythonやJava、Rなどの言語を通して利用できます。そのSAS Viyaの中で使われているのがCASTableというテーブルオブジェクトです(CASはCloud Analytic Servicesの略です)。今回はCASTableで一部のカラムだけ取り出して情報を表示する方法を紹介します。

データベースからテーブルを取得する

まずはSAS Viyaに接続します。

import swat
conn = swat.CAS('server-name.mycompany.com', 5570, 'username', 'password')

次にCASTableを取得します。今回はIRISデータのCSVを利用します。

tbl = conn.loadtable('data/iris.csv', caslib='casuser').casTable

カラムを取得する

tblをdictとして、キーを指定してカラムが取り出せます。

col = tbl['sepal_width']
col

出力すると次のようになります。

CASColumn('DATA.IRIS', caslib='CASUSER(username)')['sepal_width'].sort_values(['sepal_length', 'sepal_width'], ascending=[False, True])

データを表示する

カラムの head メソッドを使うと、そのカラムの値だけが出力されます。

col.head()
0    3.8
1    2.6
2    2.8
3    3.0
4    3.8
Name: sepal_width, dtype: float64

複数カラム取得する

同様にキーを配列で与えれば複数カラム取得できます。

widths = tbl[['sepal_width', 'petal_width', 'species']]

内容は次のようになります。

sepal_width petal_width species
0 3.8 2.0 virginica
1 2.6 2.3 virginica
2 2.8 2.0 virginica
3 3.0 2.3 virginica
4 3.8 2.2 virginica

データの概要も確認できます。

widths.describe()
sepal_width petal_width
count 150.000000 150.000000
mean 3.054000 1.198667
std 0.433594 0.763161
min 2.000000 0.100000
25% 2.800000 0.300000
50% 3.000000 1.300000
75% 3.300000 1.800000
max 4.400000 2.500000

同様にカラム情報を表示します。

widths.columninfo()
Column ID Type RawLength FormattedLength NFL NFD
0 sepal_width 2 double 8 12 0 0
1 petal_width 4 double 8 12 0 0
2 species 5 varchar 10 10 0 0

まとめ

一部のカラムを取得すれば、カラムが多数あるテーブルであっても必要なものだけに絞り込んで分析が行えます。数字データが多すぎて分析の手の付け所が分からない場合などに利用できるでしょう。

SAS for Developers | SAS

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0