0
0
Pandasdata~20 mins

str accessor for string methods in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Str Accessor Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of str.upper() on a pandas Series
What is the output of the following code?
import pandas as pd
s = pd.Series(['apple', 'Banana', 'Cherry'])
result = s.str.upper()
print(result)
Pandas
import pandas as pd
s = pd.Series(['apple', 'Banana', 'Cherry'])
result = s.str.upper()
print(result)
A
0    APPLE
1    Banana
2    CHERRY
dtype: object
B
0     APPLE
1    BANANA
2    CHERRY
dtype: object
C
0    apple
1    Banana
2    Cherry
dtype: object
D
0    apple
1    BANANA
2    cherry
dtype: object
Attempts:
2 left
💡 Hint
The str.upper() method converts all characters in each string to uppercase.
data_output
intermediate
2:00remaining
Count of strings containing 'a' using str.contains()
Given the Series below, what is the count of strings that contain the letter 'a' (case insensitive)?
import pandas as pd
s = pd.Series(['Cat', 'dog', 'Parrot', 'fish', 'ant'])
count = s.str.contains('a', case=False).sum()
print(count)
Pandas
import pandas as pd
s = pd.Series(['Cat', 'dog', 'Parrot', 'fish', 'ant'])
count = s.str.contains('a', case=False).sum()
print(count)
A3
B2
C4
D1
Attempts:
2 left
💡 Hint
Check which strings have 'a' or 'A' in them.
🔧 Debug
advanced
2:00remaining
Identify the error in using str.replace()
What error does the following code raise?
import pandas as pd
s = pd.Series(['apple', 'banana', 'cherry'])
result = s.str.replace('a', 1)
print(result)
Pandas
import pandas as pd
s = pd.Series(['apple', 'banana', 'cherry'])
result = s.str.replace('a', 1)
print(result)
AAttributeError: 'Series' object has no attribute 'str'
BValueError: invalid replacement string
CTypeError: expected string or bytes-like object
DNo error, output is ['1pple', 'b1n1n1', 'cherry']
Attempts:
2 left
💡 Hint
Check the type of the replacement argument in str.replace.
visualization
advanced
2:00remaining
Visualize string length distribution using str.len()
You have a pandas Series of words. Which code snippet correctly creates a histogram of their lengths?
import pandas as pd
import matplotlib.pyplot as plt
s = pd.Series(['dog', 'elephant', 'cat', 'hippopotamus', 'fox'])
# Which code below creates the histogram?
Pandas
import pandas as pd
import matplotlib.pyplot as plt
s = pd.Series(['dog', 'elephant', 'cat', 'hippopotamus', 'fox'])
A
plt.hist(s.str.len())
plt.show()
B
plt.hist(s.len())
plt.show()
C
plt.hist(len(s))
plt.show()
D
plt.hist(s.str.length())
plt.show()
Attempts:
2 left
💡 Hint
Use the str accessor to get string lengths.
🚀 Application
expert
3:00remaining
Filter DataFrame rows where a column's strings start with a vowel
Given the DataFrame below, which code correctly filters rows where the 'Name' column starts with a vowel (a, e, i, o, u), case insensitive?
import pandas as pd
df = pd.DataFrame({'Name': ['Alice', 'bob', 'Eve', 'Oscar', 'Uma', 'Charlie']})
Pandas
import pandas as pd
df = pd.DataFrame({'Name': ['Alice', 'bob', 'Eve', 'Oscar', 'Uma', 'Charlie']})
A
filtered = df[df['Name'].str.match('[aeiou]$', case=False)]
print(filtered)
B
filtered = df[df['Name'].str.startswith('aeiou')]
print(filtered)
C
filtered = df[df['Name'].str.contains('^[aeiou]', case=True)]
print(filtered)
D
filtered = df[df['Name'].str.match('^[aeiou]', case=False)]
print(filtered)
Attempts:
2 left
💡 Hint
Use a regex pattern with str.match to check the start of the string.