0

I have a column of my dataframe data that contains date and value information in a long string. The column, we will call x for this purpose, is formatted as such:

x = "{date1:val1, date2:val2, date3:val3, ...}" 

I want to ultimately explode this data such that I create two new columns, one for date and one for val. In order to utilize the explode function, I understand that the column must be formatted as an array, not a string. So far, to handle this issue, I have removed the braces at the start and end of the string:

from pyspark.sql import functions as F

data = data.withColumn('x_1', F.regexp.replace('x', r'\{', ''))
data = data.withColumn('x_1', F.regexp.replace('x_1', r'\}', '') 

I then created a list variable:

data = data.withColumn('x_list', F.split('x_1', ', '))

I now have that x_list = [date1:val1, date2:val2, date3:val3, ...]

What I now need is to add quotes around each list element such that I ultimately get ['date1':'val1', 'date2':'val2', 'date3':'val3', ...]

I believe that it may be possible to iterate through the list and use regex to add quotes using the colon (:) as a split point, but I am struggling with how to do that. I believe that it would look something like:

for l in x_list:
   #some regex expression

Alternatively, I have considered creating a sublist of each list element, but I am not sure how I would then use those sublists to create two new columns.

1
  • Use ast.literal_eval() to parse it as a dictionary. Then you can call pd.DataFrame() with the result. Commented Dec 13, 2024 at 20:13

2 Answers 2

0

This way you could avoid using udf :)

date_val_string = "{date1:val1, date2:val2, date3:val3}"
(
    spark.createDataFrame(pd.DataFrame({"col": [date_val_string]}))
    .withColumn("array1", f.expr("split(regexp_replace(col, '[{}]', ''), ', ')"))
    .withColumn("array2", f.expr("transform(array1, x->split(x, ':'))"))
    .selectExpr("explode(array2) as date_val")
    .selectExpr("date_val[0] as date", "date_val[1] as val")
    .show(truncate = False)
)


+-----+----+
|date |val |
+-----+----+
|date1|val1|
|date2|val2|
|date3|val3|
+-----+----+
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This is exactly what I was looking for!
0

Your string is not a valid json and also not a valid dict string. You could go with this:

import pyspark.sql.functions as F
from pyspark.sql.types import StringType, ArrayType

@F.udf(returnType=ArrayType(StringType()))
def parse_(s):
    if s is None: return None
    return [item.split(":")[1] for item in s.strip("{}").split(",")]

df = spark.createDataFrame([[1, "{date1:val1, date2:val2, date3:val3}"]], schema=["col1", "col2"])
display(df.withColumn("date", F.explode(parse_("col2"))))

2 Comments

In this line of code: df = spark.createDataFrame([[1, "{date1:val1, date2:val2, date3:val3}"]], schema=["col1", "col2"]) , what is the part in quotes? Is that the name of the column that I want to explode?
"col1" and "col2" are column names in my test data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.