0

I have a JSON file and I want to do some ETL tasks. I want to extract a column and append its values as new rows in the data frame. for example, if I have a data frame like this:

-----------------------------------------------------------------
|name    |    last    |                  father                 |
-----------------------------------------------------------------
| daniel |  allardice | {'name': 'george', 'last': 'allardice'} |
-----------------------------------------------------------------

I want to turn it to:

----------------------------
|    name    |    last     |
----------------------------
|   daniel   |  allardice  |
----------------------------
|   george   |  allardice  |
----------------------------

How can I do this by UDF in PySpark?

1 Answer 1

1

Can you try with the below code

from pyspark.sql import functions as F

df_1 = df.select("name","last");

df_2 = df.select(F.col('father').getItem('name').alias('name'), F.col('father')['last'].alias('last'));

result = df_1.union(df_2);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. What can i do if instead of JSON it would be a Row() type?

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.