0
0
Pandasdata~15 mins

str.split() for splitting in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
Splitting Text Data Using str.split() in pandas
📖 Scenario: You work in a small company that keeps customer information in a spreadsheet. One column contains full names, but you want to separate the first and last names into two columns for easier analysis.
🎯 Goal: Learn how to use pandas str.split() to split a column of full names into separate first and last name columns.
📋 What You'll Learn
Create a pandas DataFrame with a column of full names
Create a variable for the separator character
Use str.split() with the separator to split the full names into two columns
Print the resulting DataFrame with the new columns
💡 Why This Matters
🌍 Real World
Splitting full names into first and last names is common in customer data cleaning and preparation for analysis or mailing.
💼 Career
Data analysts and data scientists often need to clean and transform text data using pandas string methods like <code>str.split()</code>.
Progress0 / 4 steps
1
Create a pandas DataFrame with full names
Import pandas as pd and create a DataFrame called df with one column named 'FullName' containing these exact values: 'Alice Johnson', 'Bob Smith', 'Charlie Brown'.
Pandas
Need a hint?

Use pd.DataFrame() with a dictionary where the key is 'FullName' and the value is a list of the full names.

2
Create a separator variable
Create a variable called separator and set it to a single space character ' ' to use for splitting the full names.
Pandas
Need a hint?

The separator is a string with one space: ' '.

3
Split the FullName column into FirstName and LastName
Use df['FullName'].str.split(separator, expand=True) to split the full names into two columns. Assign the result to two new columns in df called 'FirstName' and 'LastName'.
Pandas
Need a hint?

Use expand=True to get two separate columns from the split.

4
Print the DataFrame with split names
Write a print(df) statement to display the DataFrame with the new 'FirstName' and 'LastName' columns.
Pandas
Need a hint?

The output should show the original FullName column plus the new FirstName and LastName columns.