Get startedGet started for free

Column operations - creating and renaming columns

The census dataset is still not quite showing everything you want it to. Let's make a new synthetic column by adding a new column based on existing columns, and rename it for clarity.

Remember, there's already a SparkSession called spark in your workspace!

This exercise is part of the course

Introduction to PySpark

View Course

Exercise instructions

  • Create a new column, "weekly_salary", by dividing the "income" column by 52.
  • Rename the "age" column to "years".
  • Show the resulting DataFrame.

Hands-on interactive exercise

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

# Create a new column 'weekly_salary'
census_df_weekly = census_df.____(____, ____)

# Rename the 'age' column to 'years'
census_df_weekly = ____.____(____, ____)

# Show the result
census_df_weekly.____
Edit and Run Code