【发布时间】:2020-05-18 18:42:44
【问题描述】:
我目前在将 Python Pandas 函数转换为 Python PySpark 时遇到问题,因为两者都是不同的库。我想做的是有一个查询功能,然后将其应用回同一列。
这是我为 Python Pandas 所做的(年龄是我试图从中检索的数据集中的列):
Age = [1, 3, -100, -99999, 39, 60, 87, 20, 21, 77777]
def clean_age(Age):
if Age>=0 and Age<=95:
return Age
else:
return np.nan
df['Age'] = df['Age'].apply(clean_age)
它适用于 Python Pandas,但现在这是我为 Python PySpark 所做的,但它不起作用:
from pyspark.sql.types import IntegerType, IntegerType
from pyspark.sql.functions import udf
def clean_age(Age):
if Age>=0 and Age<=95:
return Age
else:
return NaN
spark.udf.register("clean_age", clean_age)
udf_myFunction = udf(clean_age, IntegerType())
new_df2 = new_df.withColumn('Age_Clean',udf_myFunction('Age'))
new_df2.show()
请告知我如何实现从 Pandas 到 PySpark 的功能。提前致谢!
【问题讨论】:
标签: python pandas apache-spark pyspark