'Groupby and pivot in pyspark

I have a dataframe in pyspark as per below: enter image description here

I want the desired output as per below: enter image description here

what is the best way to do it?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source