0
0
Pandasdata~20 mins

Why string operations matter in Pandas - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
String Operations Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of pandas string split and expand
What is the output of this code snippet that splits a column of full names into first and last names?
Pandas
import pandas as pd

df = pd.DataFrame({'Name': ['Alice Smith', 'Bob Jones', 'Carol Lee']})
df[['First', 'Last']] = df['Name'].str.split(' ', expand=True)
df
A
       Name  First   Last
0  Alice Smith  Alice  Smith
1    Bob Jones    Bob  Jones
2   Carol Lee  Carol   Lee
B
       Name  First
0  Alice Smith  Alice
1    Bob Jones    Bob
2   Carol Lee  Carol
C[['Alice', 'Smith'], ['Bob', 'Jones'], ['Carol', 'Lee']]
D
       Name
0  Alice Smith
1    Bob Jones
2   Carol Lee
Attempts:
2 left
💡 Hint
Look at how the split with expand=True creates new columns in the DataFrame.
data_output
intermediate
1:30remaining
Count of rows containing a substring
Given this DataFrame, how many rows contain the substring 'cat' in the 'Animal' column?
Pandas
import pandas as pd

df = pd.DataFrame({'Animal': ['cat', 'dog', 'caterpillar', 'bird', 'scatter']})
count = df['Animal'].str.contains('cat').sum()
count
A3
B2
C4
D1
Attempts:
2 left
💡 Hint
Check which strings have 'cat' anywhere inside them.
🔧 Debug
advanced
1:30remaining
Identify the error in string replacement
What error does this code raise when trying to replace 'dog' with 'cat' in the 'Animal' column?
Pandas
import pandas as pd

df = pd.DataFrame({'Animal': ['dog', 'dogfish', 'hotdog']})
df['Animal'].str.replace('dog')
AValueError: pattern not found
BTypeError: replace() missing 1 required positional argument: 'repl'
CAttributeError: 'Series' object has no attribute 'str'
DNo error, returns replaced strings
Attempts:
2 left
💡 Hint
Check the required arguments for str.replace method.
visualization
advanced
2:30remaining
Visualizing string length distribution
Which option produces a histogram of the lengths of strings in the 'Words' column?
Pandas
import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({'Words': ['apple', 'banana', 'pear', 'kiwi', 'grape']})

lengths = df['Words'].str.len()
plt.hist(lengths)
plt.xlabel('Length of word')
plt.ylabel('Frequency')
plt.title('Histogram of word lengths')
plt.show()
AA line plot of word lengths over index
BA bar chart of word counts
CA scatter plot of word lengths vs words
DA histogram showing word lengths with bars at lengths 4, 5, 6
Attempts:
2 left
💡 Hint
Histogram groups numeric values into bins and shows frequency.
🧠 Conceptual
expert
2:00remaining
Why string operations are crucial in data science
Which statement best explains why string operations matter in data science?
AString operations are mainly used to speed up numerical calculations in datasets.
BString operations are only useful for formatting output and have little impact on data analysis.
CString operations allow cleaning and transforming text data, which is often messy and unstructured, enabling better analysis.
DString operations replace the need for statistical methods in data science.
Attempts:
2 left
💡 Hint
Think about the role of text data in real-world datasets.