Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PySpark: creating aggregated columns out of a string type column different values

I have this dataframe:

+---------+--------+------+
|    topic| emotion|counts|
+---------+--------+------+
|    dog  | sadness|   4  |
|    cat  |surprise|   1  |
|    bird |    fear|   3  |
|    cat  |     joy|   2  |
|    dog  |surprise|  10  |
|    dog  |surprise|   3  |
+---------+--------+------+

And I want to create a column for every different emotion aggregating the counts for every topic and every emotion, ending up having an output like this:

+---------+--------+---------+-----+----------+
|    topic| fear   | sadness | joy | surprise |
+---------+--------+---------+-----+----------+
|    dog  |    0   |   4     |  0  |     13   |
|    cat  |    0   |   0     |  2  |     1    |
|    bird |    3   |   0     |  0  |     0    |
+---------+--------+---------+-----+----------+

This is what I tried so far, for the fear column but the rest of the emotions keep showing up for every topic, how can I get a result like the above?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

agg_emotion = df.groupby("topic", "emotion") \
    .agg(F.sum(F.when(F.col("emotion").eqNullSafe("fear"), 1)\
              .otherwise(0)).alias('fear'))

>Solution :

groupy sum then groupby pivot the outcome

df.groupby('topic','emotion').agg(sum('counts').alias('counts')).groupby('topic').pivot('emotion').agg(F.first('counts')).na.fill(0).show()

+-----+----+---+-------+--------+
|topic|fear|joy|sadness|surprise|
+-----+----+---+-------+--------+
|  dog|   0|  0|      4|      13|
|  cat|   0|  2|      0|       1|
| bird|   3|  0|      0|       0|
+-----+----+---+-------+--------+
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading