mapInPandas
現在のDataFrame内のイテレータを、 Pandas DataFramesを入力および出力として実行するPythonネイティブ関数を使用してマッピングし、結果をDataFrameとして返します。
構文
mapInPandas(func: "PandasMapIterFunction", schema: Union[StructType, str], barrier: bool = False, profile: Optional[ResourceProfile] = None)
パラメーター
パラメーター | Type | 説明 |
|---|---|---|
| function |
|
| データ型または文字列 | PySpark における |
| bool型、オプション、デフォルト値はFalse | バリアモード実行を使用することで、ステージ内のすべてのPythonプロセスが同時に起動されることを保証します。 |
| リソースプロファイル(オプション) | 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)]