Pyspark: 辞書を検索して列の値を置き換える 質問する

Pyspark: 辞書を検索して列の値を置き換える 質問する

私はPySparkの初心者です。

DataFrame df「device_type」列を持つSpark があります。

「タブレット」または「電話」にあるすべての値を「電話」に置き換え、「PC」を「デスクトップ」に置き換えます。

Pythonでは次のことができます。

deviceDict = {'Tablet':'Mobile','Phone':'Mobile','PC':'Desktop'}
df['device_type'] = df['device_type'].replace(deviceDict,inplace=False)

PySpark を使用してこれを実現するにはどうすればよいでしょうか? ありがとうございます!

ベストアンサー1

次のいずれかを使用できますna.replace:

df = spark.createDataFrame([
    ('Tablet', ), ('Phone', ),  ('PC', ), ('Other', ), (None, )
], ["device_type"])

df.na.replace(deviceDict, 1).show()
+-----------+
|device_type|
+-----------+
|     Mobile|
|     Mobile|
|    Desktop|
|      Other|
|       null|
+-----------+

またはマップリテラル:

from itertools import chain
from pyspark.sql.functions import create_map, lit

mapping = create_map([lit(x) for x in chain(*deviceDict.items())])


df.select(mapping[df['device_type']].alias('device_type'))
+-----------+
|device_type|
+-----------+
|     Mobile|
|     Mobile|
|    Desktop|
|       null|
|       null|
+-----------+

後者のソリューションでは、マッピングに存在しない値が に変換されることに注意してくださいNULL。この動作が望ましくない場合は、 を追加できますcoalesce

from pyspark.sql.functions import coalesce


df.select(
    coalesce(mapping[df['device_type']], df['device_type']).alias('device_type')
)
+-----------+
|device_type|
+-----------+
|     Mobile|
|     Mobile|
|    Desktop|
|      Other|
|       null|
+-----------+

おすすめ記事