Challenge - 5 Problems
Str Accessor Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
The str.upper() method converts all characters in each string to uppercase.
✗ Incorrect
The str.upper() method applied to a pandas Series converts every string element to uppercase letters.
❓ data_output
intermediate2: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)
Attempts:
2 left
💡 Hint
Check which strings have 'a' or 'A' in them.
✗ Incorrect
The strings 'Cat', 'Parrot', and 'ant' contain 'a' or 'A', so the count is 3.
🔧 Debug
advanced2: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)
Attempts:
2 left
💡 Hint
Check the type of the replacement argument in str.replace.
✗ Incorrect
The replacement argument must be a string; passing an integer causes a TypeError.
❓ visualization
advanced2: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'])
Attempts:
2 left
💡 Hint
Use the str accessor to get string lengths.
✗ Incorrect
The str.len() method returns the length of each string element, which can be plotted.
🚀 Application
expert3: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']})
Attempts:
2 left
💡 Hint
Use a regex pattern with str.match to check the start of the string.
✗ Incorrect
str.match with regex '^[aeiou]' and case=False matches strings starting with any vowel ignoring case.