pandas DataFrames에서 입력 및 출력으로 수행되는 Python 네이티브 함수를 사용하여 현재 DataFrame의 일괄 처리 반복기를 매핑하고 결과를 DataFrame으로 반환합니다.
문법
mapInPandas(func: "PandasMapIterFunction", schema: Union[StructType, str], barrier: bool = False, profile: Optional[ResourceProfile] = None)
매개 변수
| 매개 변수 | 유형 | 설명 |
|---|---|---|
func |
기능 |
pandas.DataFrame 반복기를 사용하고 pandas.DataFrame 반복기를 출력하는 Python 네이티브 함수입니다. |
schema |
DataType 또는 str | PySpark의 func 반환 형식입니다. 값은 개체 또는 DDL 형식 형식 문자열일 수 있습니다 pyspark.sql.types.DataType . |
barrier |
bool, 선택 사항, 기본 False | 장벽 모드 실행을 사용하여 단계의 모든 Python 작업자가 동시에 시작되도록 합니다. |
profile |
ResourceProfile, 선택 사항 | mapInPandas에 사용할 선택적 ResourceProfile입니다. |
Returns
DataFrame
예제
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)]