Bird
Raised Fist0
Pandasdata~20 mins

str.split() for splitting in Pandas - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Str Split Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of splitting a pandas Series with default separator
What is the output of the following code snippet?
Pandas
import pandas as pd
s = pd.Series(['apple,banana,orange', 'cat,dog', 'red,blue'])
s_split = s.str.split()
print(s_split)
A
0    [apple, banana, orange]
1           [cat, dog]
2           [red, blue]
dtype: object
B
tcejbo :epytd
]eulb ,der[           2
]god ,tac[           1
]egnaro ,ananab ,elppa[    0
C
tcejbo :epytd
]eulb,der[           2
]god,tac[           1
]egnaro,ananab,elppa[    0
D
0    [apple,banana,orange]
1           [cat,dog]
2           [red,blue]
dtype: object
Attempts:
2 left
💡 Hint
Remember that str.split() without arguments splits on whitespace by default.
❓ data_output
intermediate
2:00remaining
Splitting a pandas Series with a comma separator
What is the output of splitting the Series by comma using str.split(",")?
Pandas
import pandas as pd
s = pd.Series(['apple,banana,orange', 'cat,dog', 'red,blue'])
s_split = s.str.split(",")
print(s_split)
A
0    [apple, banana, orange]
1           [cat, dog]
2           [red, blue]
dtype: object
B
0    [apple,banana,orange]
1           [cat,dog]
2           [red,blue]
dtype: object
C
0    [apple banana orange]
1           [cat dog]
2           [red blue]
dtype: object
D
0    apple
1    banana
2    orange
dtype: object
Attempts:
2 left
💡 Hint
Use the comma as the separator to split the strings.
❓ visualization
advanced
2:30remaining
Visualizing split results with expand=True
What is the DataFrame output when splitting the Series with expand=True?
Pandas
import pandas as pd
s = pd.Series(['apple,banana,orange', 'cat,dog', 'red,blue'])
s_split_df = s.str.split(",", expand=True)
print(s_split_df)
A
0    apple
1    banana
2    orange
dtype: object
B
        0       1       2
0   apple  banana  orange
1     cat     dog    None
2     red    blue    None
C
0    [apple, banana, orange]
1           [cat, dog]
2           [red, blue]
dtype: object
D
        0       1       2
0   apple  banana  orange
1     cat     dog     dog
2     red    blue    blue
Attempts:
2 left
💡 Hint
expand=True returns a DataFrame with each split part in its own column.
🔧 Debug
advanced
2:00remaining
Identify the error in using str.split with regex separator
What error does the following code raise?
Pandas
import pandas as pd
s = pd.Series(['apple1banana2orange', 'cat3dog', 'red4blue'])
s_split = s.str.split(r'\d')
print(s_split)
ANo error, output is lists split at digits
BTypeError: unhashable type: 'list'
CValueError: regex pattern error
DAttributeError: 'Series' object has no attribute 'str'
Attempts:
2 left
💡 Hint
Check if str.split supports regex patterns by default.
🚀 Application
expert
2:30remaining
Extracting first word from a pandas Series using str.split
Given a Series of sentences, which code correctly extracts the first word of each sentence?
Pandas
import pandas as pd
s = pd.Series(['Data science is fun', 'Python is great', 'Split strings easily'])
As.str.split().str[-1]
Bs.str.split(',').str[0]
Cs.str.split().str[0]
Ds.str.split(' ')[1]
Attempts:
2 left
💡 Hint
Use str.split() to split by whitespace and select the first element.

Practice

(1/5)
1. What does the str.split() method do in pandas when applied to a text column?
easy
A. It breaks the text into parts based on a separator.
B. It merges multiple columns into one string.
C. It converts numbers to strings.
D. It removes all spaces from the text.

Solution

  1. Step 1: Understand the purpose of str.split()

    The method splits strings into parts using a separator, like spaces or commas.
  2. Step 2: Compare with other options

    Options A, B, and C describe different string operations, not splitting.
  3. Final Answer:

    It breaks the text into parts based on a separator. -> Option A
  4. Quick Check:

    Split means break text apart [OK]
Hint: Split means break text into pieces by separator [OK]
Common Mistakes:
  • Confusing split with join or merge
  • Thinking split removes spaces instead of using them as separators
  • Assuming split converts data types
2. Which of the following is the correct syntax to split a pandas column 'Name' by spaces into multiple columns?
easy
A. df['Name'].split(expand=True)
B. df['Name'].split(' ')
C. df.str.split(' ', 'Name')
D. df['Name'].str.split(' ', expand=True)

Solution

  1. Step 1: Recall pandas string method syntax

    Use str.split() on a Series with expand=True to get multiple columns.
  2. Step 2: Check each option

    df['Name'].split(' ') misses str, C has wrong argument order, D misses separator argument.
  3. Final Answer:

    df['Name'].str.split(' ', expand=True) -> Option D
  4. Quick Check:

    Use str.split with expand=True [OK]
Hint: Use df['col'].str.split(sep, expand=True) for columns [OK]
Common Mistakes:
  • Forgetting to use .str before split
  • Not setting expand=True to get multiple columns
  • Passing wrong arguments or order
3. Given the DataFrame:
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?
medium
A. A DataFrame with columns FullName, First, Last where First and Last contain split names.
B. An error because split cannot create multiple columns.
C. A DataFrame with only First and Last columns, FullName removed.
D. A DataFrame with FullName column unchanged and First column with full names.

Solution

  1. Step 1: Understand the split with expand=True

    Splitting 'FullName' by space creates two columns: 'First' and 'Last'.
  2. Step 2: Check DataFrame structure after assignment

    Original 'FullName' stays, new columns 'First' and 'Last' are added with split parts.
  3. Final Answer:

    A DataFrame with columns FullName, First, Last where First and Last contain split names. -> Option A
  4. Quick Check:

    Split with expand=True adds columns [OK]
Hint: expand=True adds new columns, original stays [OK]
Common Mistakes:
  • Expecting original column to be removed
  • Thinking split returns a list inside one column
  • Confusing expand=True effect
4. What is wrong with this code snippet?
df['City', 'State'] = df['Location'].str.split(',', expand=True)
medium
A. expand=True should be False to assign multiple columns.
B. The split separator ',' is incorrect for splitting locations.
C. You cannot assign multiple columns using a tuple as column names like ('City', 'State').
D. The code is correct and will work without errors.

Solution

  1. Step 1: Check assignment syntax for multiple columns

    Assigning multiple columns requires a list of column names, not a tuple.
  2. Step 2: Identify correct syntax

    Use df[['City', 'State']] = ... with double brackets for multiple columns.
  3. Final Answer:

    You cannot assign multiple columns using a tuple as column names like ('City', 'State'). -> Option C
  4. Quick Check:

    Use double brackets for multiple columns [OK]
Hint: Use double brackets [[]] to assign multiple columns [OK]
Common Mistakes:
  • Using single brackets with tuple for multiple columns
  • Confusing expand=True with assignment syntax
  • Assuming tuple works like list for columns
5. You have a DataFrame with a column '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?
hard
A. Use str.split(',', expand=True) to create multiple columns.
B. Use str.split(',').explode() to split and expand tags into rows.
C. Use str.split(',').apply(list) to convert tags to lists.
D. Use str.split(',').join() to merge tags back.

Solution

  1. Step 1: Understand the goal to expand tags into rows

    Splitting creates lists; explode() turns list elements into separate rows.
  2. Step 2: Evaluate options for correct method

    Use str.split(',').explode() to split and expand tags into rows. correctly chains str.split() with explode() to get one tag per row.
  3. Final Answer:

    Use str.split(',').explode() to split and expand tags into rows. -> Option B
  4. Quick Check:

    Split then explode = rows per item [OK]
Hint: Split then explode to get rows per list item [OK]
Common Mistakes:
  • Using expand=True creates columns, not rows
  • Forgetting to use explode() after split
  • Confusing join() with explode()
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard