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

How to count unique values on pyspark

Here’s my dataset

Food     Day
Chiken   2
Chiken   6
Chiken   2
Beef     3
Chiken   4
Beef     6
Beef     7

My Output

Food        Day_Count
Chicken            3
Beef               3

Chicken is 3, because it shows in day 2,4,6
and
Beef is 3, because it shows in day 3,6,7

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

>Solution :

Your question is vague. I presume you want to find get unique days for each group of food?

df.groupBy('Food').agg(countDistinct('Day').alias('count')).show()
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