I have some example data:
my_data = [{'id': '001', 'name': 'Sam', 'class': "classA", 'age': 15, 'exam_score': 90},
{'id': '002', 'name': 'Tom', 'class': "classA", 'age': 15, 'exam_score': 78},
{'id': '003', 'name': 'Ben', 'class': "classB", 'age': 16, 'exam_score': 91},
{'id': '004', 'name': 'Max', 'class': "classB", 'age': 16, 'exam_score': 76},
{'id': '005', 'name': 'Ana', 'class': "classA", 'age': 15, 'exam_score': 88},
{'id': '006', 'name': 'Ivy', 'class': "classA", 'age': 16, 'exam_score': 77},
{'id': '007', 'name': 'Eva', 'class': "classB", 'age': 15, 'exam_score': 86},
{'id': '008', 'name': 'Zoe', 'class': "classB", 'age': 16, 'exam_score': 89}]
my_rdd = sc.parallelize(my_data)
my_rdd
Let's say I have some simple function:
def divide_by_100(value):
return value/100
The goal is to divide all exam scores by 100 with the function, and then find the minimum score. My idea is to:
- filter my_rdd by key, such that that only values in
exam_scoreremain - apply the
divide_by_100()function to this - use the
.min()and.collect()function to print the lowest exam score in the data
I'm aware that groupByKey() could also be used.
The problem is that I don't know how to put this into practice using pyspark. Would appreciate any help with this.