cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

How to merge all the columns into one column as JSON?

AmanSehgal
Honored Contributor III

I have a task to transform a dataframe.

The task is to collect all the columns in a row and embed it into a JSON string as a column.

Source DF:

imageTarget DF:

image 

1 ACCEPTED SOLUTION

Accepted Solutions

AmanSehgal
Honored Contributor III

I was able to do this by converting df to rdd and then by applying map function to it.

rdd_1 = df.rdd.map(lambda row: 
                             (row['ID'],
                             row.asDict()
                             )
              )
 
rdd_2_df = rdd_1.toDF(['ID', 'Data'])

View solution in original post

1 REPLY 1

AmanSehgal
Honored Contributor III

I was able to do this by converting df to rdd and then by applying map function to it.

rdd_1 = df.rdd.map(lambda row: 
                             (row['ID'],
                             row.asDict()
                             )
              )
 
rdd_2_df = rdd_1.toDF(['ID', 'Data'])

Join Us as a Local Community Builder!

Passionate about hosting events and connecting people? Help us grow a vibrant local community—sign up today to get started!

Sign Up Now