Get startedGet started for free

Aggregating in RDDs

Now that you have conducted analytics with DataFrames in PySpark, let's briefly do a similar task with an RDD. Using the provided code, get the sum of the values of an RDD in PySpark.

A Spark session called spark has already been made for you.

This exercise is part of the course

Introduction to PySpark

View Course

Exercise instructions

  • Create an RDD from the provided DataFrame.
  • Apply the provided Lambda Function to the keys of the RDD.
  • Collect the results of the aggregation.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# DataFrame Creation
data = [("HR", "3000"), ("IT", "4000"), ("Finance", "3500")]
columns = ["Department", "Salary"]
df = spark.createDataFrame(data, schema=columns)

# Map the DataFrame to an RDD
rdd = df.rdd.____(lambda row: (row["Department"], row["Salary"]))

# Apply a lambda function to get the sum of the DataFrame
rdd_aggregated = rdd.____(lambda x, y: x + y)

# Show the collected Results
print(rdd_aggregated.____())
Edit and Run Code