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 exerciseMore exercises in Cleaning
- Fill missing values in one columnPro
- Drop rows missing a key fieldPro
- Convert text to numbersPro
- Tidy up text with strip and lowerPro
- Search text with str.containsPro
- Pull numbers out of codesPro
- Remove duplicate recordsPro
- Rename columns and replace a placeholderPro
- Dates with to_datetime and dtPro
- Clean messy money textPro
- Fill gaps with interpolatePro
- Parse dates in day/month/year orderPro