0
0
Pandasdata~20 mins

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

Choose your learning style9 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.