Skip to content

2. Add a Bonus Column

Difficulty: Easy · Topics: Filtering & Selection

Add two new columns to employees:

Return the columns id, name, salary, bonus, total_comp. If salary is null, both new columns should be null.

Row order does not matter; column names must match. Your code is graded on 3 test cases, including hidden edge cases.

Sample data

employees

idnamesalary
1Asha72000
2Ben48000
3Chitra95000

Expected output

idnamesalarybonustotal_comp
1Asha72000720079200
2Ben48000480052800
3Chitra950009500104500

Hints

Hint 1withColumn("bonus", F.col("salary") * 0.1) creates a new column.
Hint 2A column you just created can be used by the next withColumn.
Hint 3Arithmetic with null gives null automatically.

Learn the concepts

PySpark functions you'll practise

Related problems

Browse

Topics: Window Functions · Joins · Aggregations · Pivot, Unpivot & Rollup · Arrays · Null Handling · Conditional Logic · Dates · Filtering & Selection · Strings · Data Lake · Lakehouse · Streaming · Data Prep for AI · Spark Performance

Difficulty: Easy · Medium · Hard · Learn · All problems · dbt practice