We use str.split() to break text into smaller parts. This helps us work with each piece separately.
str.split() for splitting in Pandas
Series.str.split(pat=None, n=-1, expand=False)
pat is the character or pattern to split on. If None, splits on whitespace.
n limits the number of splits. Default -1 means no limit.
expand=True returns a DataFrame with split parts as columns.
df['col'].str.split()
df['col'].str.split(',')
df['col'].str.split('-', n=1)
df['col'].str.split(',', expand=True)
This code splits the full_name column into two columns: first_name and last_name.
import pandas as pd data = {'full_name': ['Alice Smith', 'Bob Johnson', 'Charlie Lee']} df = pd.DataFrame(data) # Split full_name into first and last name split_names = df['full_name'].str.split(' ', expand=True) split_names.columns = ['first_name', 'last_name'] print(split_names)
If you use expand=False (default), the result is a Series of lists.
Use expand=True to get a DataFrame with separate columns for each split part.
If the number of splits varies, some columns may have missing values (NaN).
str.split() breaks text into parts based on a separator.
Use expand=True to get multiple columns from one text column.
This helps analyze or clean text data by working with smaller pieces.
Practice
str.split() method do in pandas when applied to a text column?Solution
Step 1: Understand the purpose of
The method splits strings into parts using a separator, like spaces or commas.str.split()Step 2: Compare with other options
Options A, B, and C describe different string operations, not splitting.Final Answer:
It breaks the text into parts based on a separator. -> Option AQuick Check:
Split means break text apart [OK]
- Confusing split with join or merge
- Thinking split removes spaces instead of using them as separators
- Assuming split converts data types
'Name' by spaces into multiple columns?Solution
Step 1: Recall pandas string method syntax
Usestr.split()on a Series withexpand=Trueto get multiple columns.Step 2: Check each option
df['Name'].split(' ') missesstr, C has wrong argument order, D misses separator argument.Final Answer:
df['Name'].str.split(' ', expand=True) -> Option DQuick Check:
Usestr.splitwithexpand=True[OK]
- Forgetting to use .str before split
- Not setting expand=True to get multiple columns
- Passing wrong arguments or order
import pandas as pd
df = pd.DataFrame({'FullName': ['Alice Smith', 'Bob Jones', 'Carol Lee']})
df[['First', 'Last']] = df['FullName'].str.split(' ', expand=True)
print(df)What will be the output?
Solution
Step 1: Understand the split with expand=True
Splitting 'FullName' by space creates two columns: 'First' and 'Last'.Step 2: Check DataFrame structure after assignment
Original 'FullName' stays, new columns 'First' and 'Last' are added with split parts.Final Answer:
A DataFrame with columns FullName, First, Last where First and Last contain split names. -> Option AQuick Check:
Split with expand=True adds columns [OK]
- Expecting original column to be removed
- Thinking split returns a list inside one column
- Confusing expand=True effect
df['City', 'State'] = df['Location'].str.split(',', expand=True)Solution
Step 1: Check assignment syntax for multiple columns
Assigning multiple columns requires a list of column names, not a tuple.Step 2: Identify correct syntax
Use df[['City', 'State']] = ... with double brackets for multiple columns.Final Answer:
You cannot assign multiple columns using a tuple as column names like ('City', 'State'). -> Option CQuick Check:
Use double brackets for multiple columns [OK]
- Using single brackets with tuple for multiple columns
- Confusing expand=True with assignment syntax
- Assuming tuple works like list for columns
'Tags' containing strings like 'red,blue,green'. You want to create a new DataFrame where each tag is a separate row linked to the original row index. Which pandas method combined with str.split() helps achieve this?Solution
Step 1: Understand the goal to expand tags into rows
Splitting creates lists;explode()turns list elements into separate rows.Step 2: Evaluate options for correct method
Usestr.split(',').explode()to split and expand tags into rows. correctly chainsstr.split()withexplode()to get one tag per row.Final Answer:
Usestr.split(',').explode()to split and expand tags into rows. -> Option BQuick Check:
Split then explode = rows per item [OK]
- Using expand=True creates columns, not rows
- Forgetting to use explode() after split
- Confusing join() with explode()
Ready to prove it?
Pass the test and this pattern moves unknown
