LoginSignup
0
0

More than 1 year has passed since last update.

SPSS ModelerのフィルターノードをPythonで書き換える。フィールドのフィルタリングまたは名前の変更

Last updated at Posted at 2020-06-11

SPSS Modelerで列を絞り込んだり、列名を変更するフィルターノードをPythonのpandasで書き換えてみます。

1.加工のイメージ

以下のデータのUP_TIMEとERR_CDの列を削除し、
「M_CD」という列名を「MCD」に、「POWER」という列名を「VOLT」に変更します。

■加工前

image.png

■加工後

image.png

2. Modelerフィルターノードでの指定

削除したい列に×のチェックをいれ、変更したい名前を右のフィールド名の欄に書きます。
image.png

3. pandasで書き換える

列の選択はいくつかありますが、第一には下のようにリストで残したい列名を列挙する方法があります。

df1=df[['M_CD','POWER','TEMP']]

また、削除したい列を指定するというdropというメソッドもあります。こちらの方がModelerのフィルターのイメージに近いかもしれません。

df1_1=df.drop(['UP_TIME','ERR_CD'],axis=1)

どちらの方法でも以下のような結果が返ります。

image.png

次に列名の変更はrenameというメソッドが使えます。変更前と変更後の列名のセットをディクショナリで与えます。

df2=df1.rename(columns={'M_CD': 'MCD', 'POWER': 'VOLT'})

以下のような結果が返ります。

image.png

4. サンプル

サンプルは以下に置きました。

ストリーム
https://github.com/hkwd/200611Modeler2Python/raw/master/filternode/FilterNode.str
notebook
https://github.com/hkwd/200611Modeler2Python/blob/master/filternode/FilterNode2.ipynb
データ
https://raw.githubusercontent.com/hkwd/200611Modeler2Python/master/data/Cond4n_e.csv

■テスト環境
Modeler 18.2.1
Windows 10 64bit
Python 3.6.9
pandas 0.24.1

5.参考サイト

pandasのインデックス参照で行・列を選択し取得 | note.nkmk.me
https://note.nkmk.me/python-pandas-index-row-column/
pandas.DataFrameの行名・列名の変更 | note.nkmk.me
https://note.nkmk.me/python-pandas-dataframe-rename/

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