Pandasdrill

Level 2: Cleaning Exercise 22 of 63 Pro

Split a column into two

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

Split `full_name` into a `first` column (the text before the first space) and a `last` column (everything after it, so a two-part surname stays together). Return a DataFrame with just those two columns.

What this exercise teaches

n=1 splits only at the first space, so "Ben Ortiz Lee" becomes "Ben" and "Ortiz Lee". Without n, a three-word name produces a third column and the assignment of two names fails. expand=True builds the DataFrame; the integer column labels then need renaming.

A hint if you get stuck

str.split takes n, the maximum number of splits, and expand=True returns one column per piece. The columns come back numbered 0 and 1, so rename them.

Try it now, free, no account needed

The first 15 tasks 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 adds timed mode and the certificate.

Open this exercise

More exercises in Cleaning