Pandasdrill

Level 1: Selecting and filtering Exercise 13 of 102

Add a calculated column

A pandas exercise from Pandasdrill. Solve it with a Python editor in your browser running real pandas, and it is checked the moment you submit.

The task

Add a column `monthly` that holds each salary divided by 12, and return the columns `name`, `salary` and `monthly`, in that order, for every employee.

What this exercise teaches

Dividing a column by a number is done row by row without a loop. assign adds the result as a new column on a copy, which keeps the original table untouched and lets you chain the column selection after it.

A hint if you get stuck

Arithmetic on a column works on every row at once. assign(new_name=values) returns a copy of the table with the extra column.

Try it now, free, no account needed

Your first 8 exercises on Pandasdrill are free. Open it on the practice page, solve it, and get instant feedback with a hint. A free account adds the worked solution; Pro unlocks every exercise, timed mode and the certificate.

Open this exercise

More exercises in Selecting and filtering