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 1 year has passed since last update.

DatabricksAdvent Calendar 2022

Day 20

Databricks にて 値の前後に空白を含むすべてのカラムを trim する Python関数の作成方法

Last updated at Posted at 2022-12-20

概要

Databricks にて 値の前後に空白を含むすべてのカラムを trim する Python関数を共有します。

from pyspark.sql.functions import expr
 
 
def trim_space_values(
    tgt_df,
    trim_option='BOTH',
):
    cur_dtypes = tgt_df.dtypes
 
    # `string`のカラムのみを対象とする
    tgt_cols = []
    for tgt_dtyp in cur_dtypes:
        if tgt_dtyp[1].lower() == 'string':
            tgt_cols.append(tgt_dtyp[0])
    for tgt_col in tgt_cols:
        case_state = f"trim({trim_option} FROM {tgt_col})"
        tgt_df = tgt_df.withColumn(tgt_col, expr(case_state))
    return tgt_df

Spark 3.3 以降であれば、下記の関数を利用してください。

from pyspark.sql.functions import expr


def trim_space_values(
    tgt_df,
    trim_option='BOTH',
):
    cur_dtypes = tgt_df.dtypes

    # `string`のカラムのみを対象とする
    tgt_cols = []
    for tgt_dtyp in cur_dtypes:
        if tgt_dtyp[1].lower() == 'string':
            tgt_cols.append(tgt_dtyp[0])
    with_conds = {}
    for tgt_col in tgt_cols:
        case_state = f"trim({trim_option} FROM {tgt_col})"
        with_conds[tgt_col] = expr(case_state)
    tgt_df = tgt_df.withColumns(with_conds)
    return tgt_df

trim_optionにて、ドキュメントに記載のあるstrより前の箇所(例:LEADING)を指定できるようにしてあります。

image.png

引用元:trim 関数 (Databricks SQL) - Azure Databricks - Databricks SQL | Microsoft Learn

trim_optionにてLEADINGを指定する方法を下記に示します。

result_df = trim_space_values(df,'LEADING')
 
print(result_df.toPandas().to_dict())
result_df.display()

image.png

動作確認

1. 関数を定義

from pyspark.sql.functions import expr
 
 
def trim_space_values(
    tgt_df,
    trim_option='BOTH',
):
    cur_dtypes = tgt_df.dtypes
 
    # `string`のカラムのみを対象とする
    tgt_cols = []
    for tgt_dtyp in cur_dtypes:
        if tgt_dtyp[1].lower() == 'string':
            tgt_cols.append(tgt_dtyp[0])
    for tgt_col in tgt_cols:
        case_state = f"trim({trim_option} FROM {tgt_col})"
        tgt_df = tgt_df.withColumn(tgt_col, expr(case_state))
    return tgt_df

2. データフレームを作成

data = [
    {
        "col_001": "a",
        "col_002": "a ",
        "col_003": " a",
        "col_004": " a ",
        "col_005": 1,
    }
]
 
df = spark.createDataFrame(data)
print(df.toPandas().to_dict())

image.png

3. 関数の利用

result_df = trim_space_values(df)
 
print(result_df.toPandas().to_dict())
result_df.display()

image.png

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?