I have a dataframe as below where ev is of type string.
>>> df2.show()
+---+--------------+
| id| ev|
+---+--------------+
| 1| 200, 201, 202|
| 1|23, 24, 34, 45|
| 1| null|
| 2| 32|
| 2| null|
+---+--------------+
Is there a way to cast ev to type ArrayType without using UDF or UDF is the only option to do that?
You can use built-in split
function:
from pyspark.sql.functions import col, split
df = sc.parallelize([
(1, "200, 201, 202"), (1, "23, 24, 34, 45"), (1, None),
(2, "32"), (2, None)]).toDF(["id", "ev"])
df.select(col("id"), split(col("ev"), ",\s*").alias("ev"))
If you want to convert data to numeric types you can cast as follows:
df.withColumn(
"ev",
split(col("ev"), ",\s*").cast("array<int>").alias("ev")
)
or
from pyspark.sql.types import ArrayType, IntegerType
df.withColumn(
"ev",
split(col("ev"), ",\s*").cast(ArrayType(IntegerType())).alias("ev")
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With