I tried to replace part of the column text with "?". But I got an error. I'd like to use the native dataframe in spark.
TypeError: Column is not iterable
data = [("John Doe", "123 Main St, Anytown, USA"),
("Jane Smith", "456 High St, Somewhere, USA")]
df = spark.createDataFrame(data, ["name", "address"])
from pyspark.sql.functions import regexp_replace, col
start = 3
end = 10
df_masked = df.withColumn("address_masked", regexp_replace(col("address"), substring("address", start, end), "??????????"))
expected result
name | address_masked |
---|---|
John Doe | 123????????, Anytown, USA |
Jane Smith | 456??????????, Somewhere, USA |