0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

casTableのクエリで文字列を変換する

Posted at

SAS ViyaはAIプラットフォームになります。Webブラウザ上で機械学習の設計、実行ができるStudioという環境も用意されていますが、開発者はプログラミングコードで開発することも可能です。プログラミング言語はJava/Python/R/SASが選べます。

機械学習を用いる際に専用のテーブル(casTable)を用いますが、今回はそのテーブルオブジェクト(Python版)で文字列を加工する方法を解説します。

テーブルの作り方

テーブルはCSV、HTML、他のデータベースなどから作れます。

out = sess.upload('Jupyter_Saved_Work/Data/iris.csv')

ファイルをアップロードした後、casTableとして取得します。

tbl = out.casTable

カラムを追加する

例えばカラム同士の計算結果を追加したい、レポート用に特定の文字列を出したいなど、様々な用途が考えられます。これは tbl にキーを追加するだけです。例えば以下は文字列として指定しているので、同じ文字が表示されます。

tbl['newColumn'] = 'SepalLength * 2'

計算を伴う場合は以下のようにtblを使います。

tbl['newColumn2'] = tbl.SepalLength * 2

カラムを加工する

そしてカラムの加工方法です。例えば title というメソッドを使うと単語の最初の文字を大文字に、後は小文字に変換できます。

tbl['NameTitle'] = tbl['Name'].str.title()

| SepalLength | SepalWidth | PetalLength | PetalWidth | Name | NameTitle
|---|-----|-----|-----|-----|-------------|------------|
| 0 | 5.1 | 3.5 | 1.4 | 0.2 | Iris-setosa | Iris-Setosa
| 1 | 4.9 | 3.0 | 1.4 | 0.2 | Iris-setosa | Iris-Setosa
| 2 | 4.7 | 3.2 | 1.3 | 0.2 | Iris-setosa | Iris-Setosa
| 3 | 4.6 | 3.1 | 1.5 | 0.2 | Iris-setosa | Iris-Setosa
| 4 | 5.0 | 3.6 | 1.4 | 0.2 | Iris-setosa | Iris-Setosa

特定の文字を探す

カラムの中にある文字を検索する際には find を使います。

tbl['NameFind'] = tbl['Name'].str.find('-')

| SepalLength | SepalWidth | PetalLength | PetalWidth | Name | NameFind |
|---|-----|-----|-----|-----|-------------|-----|
| 0 | 5.1 | 3.5 | 1.4 | 0.2 | Iris-setosa | 4.0 |
| 1 | 4.9 | 3.0 | 1.4 | 0.2 | Iris-setosa | 4.0 |
| 2 | 4.7 | 3.2 | 1.3 | 0.2 | Iris-setosa | 4.0 |
| 3 | 4.6 | 3.1 | 1.5 | 0.2 | Iris-setosa | 4.0 |
| 4 | 5.0 | 3.6 | 1.4 | 0.2 | Iris-setosa | 4.0 |

大文字化

セル内の文字列を大文字化します。

tbl['NameUpper'] = tbl['Name'].str.upper()

| SepalLength | SepalWidth | PetalLength | PetalWidth | Name | isNameUpper
|---|-----|-----|-----|-----|-------------|-------------|
| 0 | 5.1 | 3.5 | 1.4 | 0.2 | Iris-setosa | IRIS-SETOSA |
| 1 | 4.9 | 3.0 | 1.4 | 0.2 | Iris-setosa | IRIS-SETOSA |
| 2 | 4.7 | 3.2 | 1.3 | 0.2 | Iris-setosa | IRIS-SETOSA |
| 3 | 4.6 | 3.1 | 1.5 | 0.2 | Iris-setosa | IRIS-SETOSA |
| 4 | 5.0 | 3.6 | 1.4 | 0.2 | Iris-setosa | IRIS-SETOSA |

追加したカラムを処理に利用する

新しいカラムを追加する際には、計算したカラムを利用することもできます。以下の例では大文字化したカラムがすべて大文字かどうかを判定するカラムを追加します。

tbl['isNameUpper'] = tbl['NameUpper'].str.isupper()

| SepalLength | SepalWidth | PetalLength | PetalWidth | Name | NameUpper | isNameUpper
|---|-----|-----|-----|-----|-------------|-------------|-----|
| 0 | 5.1 | 3.5 | 1.4 | 0.2 | Iris-setosa | IRIS-SETOSA | 1.0
| 1 | 4.9 | 3.0 | 1.4 | 0.2 | Iris-setosa | IRIS-SETOSA | 1.0
| 2 | 4.7 | 3.2 | 1.3 | 0.2 | Iris-setosa | IRIS-SETOSA | 1.0
| 3 | 4.6 | 3.1 | 1.5 | 0.2 | Iris-setosa | IRIS-SETOSA | 1.0
| 4 | 5.0 | 3.6 | 1.4 | 0.2 | Iris-setosa | IRIS-SETOSA | 1.0


文字列向けの関数としては他にも count / index / slice / len / contains など多数用意されています。閲覧時の簡易的なデータ加工としてご利用ください。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?