【发布时间】:2021-09-21 20:33:15
【问题描述】:
我创建了一个 udf 来在 PySpark 数据帧的列中生成随机的十位整数:
phone_udf = F.udf(lambda: np.random.randint(low = 1111111111, high = 9999999999), T.IntegerType())
households = sc.union([sc.parallelize([[j]
for j
in np.random.choice(household_sizes, size=partition_size, p=hh_size_probs).tolist()])
for i in range(partition_count)]).toDF(["_household_members"])\
.limit(nhouseholds)\
.withColumn("household_id", F.row_number().over(w))\
.withColumn("_hoh_last_name_id", (F.rand() * name_count).cast("int"))\
.withColumn("_hh_address_id", (F.rand() * address_filtered_count).cast("int"))\
.withColumn("phone", phone_udf())
但是,生成的数据帧的“电话”列包含所有不同长度的整数,最多 10 位,包括正数和负数。我不确定为什么 np.random.randint 在 udf 中的行为不符合预期。
【问题讨论】:
标签: python dataframe numpy pyspark user-defined-functions