Challenge - 5 Problems
Str Split Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
Remember that str.split() without arguments splits on whitespace by default.
✗ Incorrect
The str.split() method without arguments splits on whitespace. Since the strings have no spaces, the entire string remains as one element in the list.
❓ data_output
intermediate2: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)
Attempts:
2 left
💡 Hint
Use the comma as the separator to split the strings.
✗ Incorrect
Using str.split(",") splits each string at commas, producing lists of words without commas and with spaces removed.
❓ visualization
advanced2: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)
Attempts:
2 left
💡 Hint
expand=True returns a DataFrame with each split part in its own column.
✗ Incorrect
The expand=True option splits the strings into separate columns. Missing values are filled with None (NaN).
🔧 Debug
advanced2: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)
Attempts:
2 left
💡 Hint
Check if str.split supports regex patterns by default.
✗ Incorrect
str.split in pandas supports regex patterns by default, so splitting on digits works without error.
🚀 Application
expert2: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'])
Attempts:
2 left
💡 Hint
Use str.split() to split by whitespace and select the first element.
✗ Incorrect
str.split() splits by whitespace by default. Using str[0] selects the first word from each list.