【发布时间】:2021-02-03 20:08:54
【问题描述】:
我有一个超过 100 列的表格。我需要从某些列中删除双引号。我找到了两种方法,使用 withColumn() 和 map()
使用 withColumn()
cols_to_fix = ["col1", ..., "col20"]
for col in cols_to_fix:
df = df.withColumn(col, regexp_replace(df[col], "\"", ""))
使用 map()
def remove_quotes(row: Row) -> Row:
row_as_dict = row.asDict()
cols_to_fix = ["col1", ..., "col20"]
for column in cols_to_fix:
if row_as_dict[column]:
row_as_dict[column] = re.sub("\"", "", str(row_as_dict[column]))
return Row(**row_as_dict)
df = df.rdd.map(remove_quotes).toDF(df.schema)
这是我的问题。我发现在有 ~25M 记录的表上使用 map() 比 withColumn() 花费大约 4 倍的时间。如果任何堆栈溢出用户能够解释性能差异的原因,我将非常感激,这样我以后就可以避免类似的陷阱。
【问题讨论】:
标签: performance dataframe pyspark rdd