0

I'm running the following code on databricks:

dataToShow = jDataJoined.\
withColumn('id', monotonically_increasing_id()).\
filter( 
  (jDataJoined.containerNumber == 'SUDU8108536')).\
select(col('id'), col('returnTemperature'), col('supplyTemperature'))

This will give me tabular data like

tabular data

Now I would like to display a line graph with this returnTemperature and supplyTemperature as categories.

As far as I understood, the method display in databricks wants as second argument the category, so basically what I should have is something like

id - temperatureCategory - value
1 - returnTemperature - 25.0
1 - supplyTemperature - 27.0
2 - returnTemperature - 24.0
2 - supplyTemperature - 28.0

How can I transform the dataframe in this way?

1 Answer 1

1

I don't know if your format is what the display method is expecting, but you can do this transformation with the sql functions create_map and explode:

#creates a example df
from pyspark.sql import functions as F
l1 = [(1,25.0,27.0),(2,24.0,28.0)]
df = spark.createDataFrame(l1,['id','returnTemperature','supplyTemperature'])

#creates a map column which contains the values of the returnTemperature and supplyTemperature
df = df.withColumn('mapCol', F.create_map(
                                    F.lit('returnTemperature'),df.returnTemperature
                                    ,F.lit('supplyTemperature'),df.supplyTemperature
                                   ) 
                  )
#The explode function creates a new row for each element of the map
df = df.select('id',F.explode(df.mapCol).alias('temperatureCategory','value'))
df.show()

Output:

+---+-------------------+-----+ 
| id|temperatureCategory|value| 
+---+-------------------+-----+ 
| 1 |  returnTemperature| 25.0| 
| 1 |  supplyTemperature| 27.0| 
| 2 |  returnTemperature| 24.0| 
| 2 |  supplyTemperature| 28.0| 
+---+-------------------+-----+
Sign up to request clarification or add additional context in comments.

Comments

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.