SAS ViyaはAIプラットフォームです。PythonやJava、Rなどの言語を通して利用できます。そのSAS Viyaの中で使われているのがCASTableというテーブルオブジェクトです(CASはCloud Analytic Servicesの略です)。今回はCASTableをPandasの中でDataFrameのように扱う方法を紹介します。
CASTableを取得する
まずはCASTableを取得します。今回はCSVをデータソースとします。
tbl = conn.loadtable('data/iris.csv', caslib='casuser').casTable
Pandasで同じようにCSVをデータソースとしてDataFrameを取得します。
df = pd.read_csv('/u/username/data/iris.csv')
カラムを出力します。
tbl.columns
# Index(['sepal_length', 'sepal_width', 'petal_length', 'petal_width',
# 'species'],
# dtype='object')
DataFrameの場合です。
df.columns
# Index(['sepal_length', 'sepal_width', 'petal_length', 'petal_width',
# 'species'],
# dtype='object')
データタイプは次のようになります。
df.dtypes
# sepal_length float64
# sepal_width float64
# petal_length float64
# petal_width float64
# species object
# dtype: object
DataFrameの場合です。
tbl.dtypes
# sepal_length double
# sepal_width double
# petal_length double
# petal_width double
# species varchar
# dtype: object
get_dtype_counts
get_dtype_countsの出力結果です。
tbl.get_dtype_counts()
# double 4
# varchar 1
# dtype: int64
df.get_dtype_counts()
# float64 4
# object 1
# dtype: int64
describe
describeの結果です。
casdesc = tbl.describe()
casdesc
sepal_length | sepal_width | petal_length | petal_width |
---|---|---|---|
count | 150.000000 | 150.000000 | 150.000000 |
mean | 5.843333 | 3.054000 | 3.758667 |
std | 0.828066 | 0.433594 | 1.764420 |
min | 4.300000 | 2.000000 | 1.000000 |
25% | 5.100000 | 2.800000 | 1.600000 |
50% | 5.800000 | 3.000000 | 4.350000 |
75% | 6.400000 | 3.300000 | 5.100000 |
max | 7.900000 | 4.400000 | 6.900000 |
desc = df.describe()
desc
sepal_length | sepal_width | petal_length | petal_width |
---|---|---|---|
count | 150.000000 | 150.000000 | 150.000000 |
mean | 5.843333 | 3.054000 | 3.758667 |
std | 0.828066 | 0.433594 | 1.764420 |
min | 4.300000 | 2.000000 | 1.000000 |
25% | 5.100000 | 2.800000 | 1.600000 |
50% | 5.800000 | 3.000000 | 4.350000 |
75% | 6.400000 | 3.300000 | 5.100000 |
max | 7.900000 | 4.400000 | 6.900000 |
まとめ
このようにCASTableはDataFrameと高い互換性を持っています。そのため、Pandasを用いてデータをグラフ化するのも可能です。皆さんのデータ分析にSAS Viya、そしてCASTableを活用してください。