Pandasdrill

Level 2: Cleaning Exercise 12 of 45 Pro

Convert text to numbers

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

The `price` column was read in as text. Convert it to float, add a `total` column equal to `qty * price`, and return the whole `items` table.

What this exercise teaches

Text that looks like numbers still has the object dtype, so arithmetic on it either fails or repeats strings. astype(float) or pd.to_numeric turns it into real numbers before you compute the total.

A hint if you get stuck

astype(float) converts a column of numeric strings. Multiplying a number by a string in Python repeats the string, which is not what you want.

This exercise is part of Pro

Pro unlocks all 45 exercises, with worked solutions and progress saved to your account. The free exercises work without an account.

You create a free account first, so Pro is attached to it.

Monthly is charged today and then every month until you cancel, in one click from your account page. Lifetime is one payment with no renewals. Full refund within 14 days of paying.

Start with the free exercises

More exercises in Cleaning