Anjikya
Anjikya

Reputation: 21

Pyspark - Converting the Json type data into tabular form

I have input data like given below in a pyspark dataframe,

enter image description here

and i want to convert it into the tabular form like below,

enter image description here

Please help me out as i am pretty new to pyspark.

Upvotes: 0

Views: 37

Answers (1)

s.polam
s.polam

Reputation: 10372

You can use from_json along with schema

df
.withColumn(
   "Results", 
   expr("from_json(Results, 'country string, gender string, city string')")
)
.selectExpr("Results.*")
.show(False)

+-------+------+-------+
|country|gender|city   |
+-------+------+-------+
|Germnay|male  |Hamburg|
|India  |male  |Delhi  |
|France |female|paris  |
|Germnay|male  |Munich |
+-------+------+-------+

Upvotes: 0

Related Questions