2
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?

正規表現で社名を名寄せするpython拡張ノードをBobに作らせる(SPSS Modeler データ加工逆引き2-24)

2
Posted at

正規表現で社名を名寄せするpython拡張ノードをBobに作らせる

スクリーンショット 2026-03-17 11.59.54.png

以下の記事を題材にBobで拡張ノードにpythonを便利に書かせる方法を紹介します。

1.想定される利用目的

・顧客マスタや取引先マスタデータのクレンジング
・対象データの精査

2.サンプルストリームのダウンロード

3.サンプルストリームの説明

スクリーンショット 2026-03-17 12.00.55.png

a.入力するデータは以下の通りです。

スクリーンショット 2026-03-17 11.57.20.png

b.[拡張の変換]ノードを編集します。

スクリーンショット 2026-03-17 13.05.26.png

こちらの記事の

ダウンロード可能ストリーム
220601正規表現python.str
の拡張ノードをそのまま使っています。記事作成当時のバージョンではPythonの選択肢はPython for Sparkのみでした。

[テーブル]を表示します。前株のみ株式会社を(株)に変換しています。

スクリーンショット 2026-03-17 13.06.21.png

c.[拡張の変換]ノードを編集します。Pythonシンタックスを使っています。後株も(株)変換するように変更をしています。Bobでどのように作成したかは後述します。

スクリーンショット 2026-03-17 13.06.57.png

[テーブル]を表示します。

スクリーンショット 2026-03-17 11.57.40.png

Bobにpythonスクリプトを書かせる方法

Bobを起動します。

スクリーンショット 2026-02-09 9.53.42.png

Start>Open で作業スペースフォルダを指定します。

スクリーンショット 2026-02-09 9.57.26.png

プロンプトは次のとおりです。あらかじめ題材にしたストリームを作業フォルダに格納しておきます。

スクリーンショット 2026-03-17 12.00.12.png

次のpythonスクリプトが生成されたらコピーして[拡張の変換]ノードにペーストします。

python

###Use this code to transfer data between Modeler and Python
import modelerpy
import pandas as pd
import re

if modelerpy.isComputeDataModelOnly():
    # データモデルの計算のみの場合
    modelerDataModel = modelerpy.getDataModel()
    outputDataModel = modelerDataModel  # 入力と同じデータモデルを出力
    modelerpy.setOutputDataModel(outputDataModel)
else:
    # 実際のデータ処理
    modelerData = modelerpy.readPandasDataframe()
    
    # 全ての文字列カラムに対して「株式会社」を「(株)」に置換
    outputData = modelerData.copy()
    
    for column in outputData.columns:
        # カラムがobject型(文字列)の場合のみ処理
        if outputData[column].dtype == 'object':
            # 正規表現を使って「株式会社」を「(株)」に置換
            # 位置に関係なく全ての出現箇所を置換
            outputData[column] = outputData[column].apply(
                lambda x: re.sub(r'株式会社', '(株)', str(x)) if pd.notna(x) else x
            )
    
    modelerpy.writePandasDataframe(outputData)

# Made with Bob



注意事項

正規表現の例としてシンプルな内容を選んでいますが、株式会社を(株)に変更するのみの場合は
[置換]ノードでreplace("株式会社","(株)", NAME)と関数を使って同じ処理が可能です。

4.参考情報

法人等略語マスタで組織名を名寄せする

SPSS Modeler ノードリファレンス目次

SPSS Modeler 逆引きストリーム集(データ加工)

2
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
2
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?