メインコンテンツまでスキップ

mapInPandas

現在のDataFrame内のイテレータを、 Pandas DataFramesを入力および出力として実行するPythonネイティブ関数を使用してマッピングし、結果をDataFrameとして返します。

構文

mapInPandas(func: "PandasMapIterFunction", schema: Union[StructType, str], barrier: bool = False, profile: Optional[ResourceProfile] = None)

パラメーター

パラメーター

Type

説明

func

function

pandas.DataFrame秒のイテレータを受け取り、 pandas.DataFrame秒のイテレータを出力する Python ネイティブ関数。

schema

データ型または文字列

PySpark におけるfuncの戻り値の型。値は、 pyspark.sql.types.DataTypeオブジェクトまたは DDL 形式の型文字列のいずれかになります。

barrier

bool型、オプション、デフォルト値はFalse

バリアモード実行を使用することで、ステージ内のすべてのPythonプロセスが同時に起動されることを保証します。

profile

リソースプロファイル(オプション)

mapInPandasで使用するオプションのリソースプロファイル。

戻り値

DataFrame

Python
df = spark.createDataFrame([(1, 21), (2, 30)], ("id", "age"))

def filter_func(iterator):
for pdf in iterator:
yield pdf[pdf.id == 1]

df.mapInPandas(filter_func, df.schema).show()
# +---+---+
# | id|age|
# +---+---+
# | 1| 21|
# +---+---+

def mean_age(iterator):
for pdf in iterator:
yield pdf.groupby("id").mean().reset_index()

df.mapInPandas(mean_age, "id: bigint, age: double").show()
# +---+----+
# | id| age|
# +---+----+
# | 1|21.0|
# | 2|30.0|
# +---+----+

df.mapInPandas(filter_func, df.schema, barrier=True).collect()
# [Row(id=1, age=21)]
このページの見出し