0
0
Data Analysis Pythondata~20 mins

String methods on Series in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
String Series Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of lowercasing a Series with mixed case strings
What is the output of this code?
import pandas as pd
s = pd.Series(['Apple', 'Banana', 'Cherry'])
result = s.str.lower()
print(result)
Data Analysis Python
import pandas as pd
s = pd.Series(['Apple', 'Banana', 'Cherry'])
result = s.str.lower()
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.lower() method converts all characters in each string to lowercase.
data_output
intermediate
2:00remaining
Count of strings containing a substring in a Series
Given this Series, what is the output of counting how many strings contain 'an'?
import pandas as pd
s = pd.Series(['banana', 'apple', 'mango', 'orange'])
count = s.str.contains('an').sum()
print(count)
Data Analysis Python
import pandas as pd
s = pd.Series(['banana', 'apple', 'mango', 'orange'])
count = s.str.contains('an').sum()
print(count)
A1
B2
C4
D3
Attempts:
2 left
💡 Hint
Check which strings have 'an' inside them.
🔧 Debug
advanced
2:00remaining
Identify the error in using str.replace with regex
What error does this code raise?
import pandas as pd
s = pd.Series(['cat', 'bat', 'rat'])
result = s.str.replace('[ab]', '', regex=False)
print(result)
Data Analysis Python
import pandas as pd
s = pd.Series(['cat', 'bat', 'rat'])
result = s.str.replace('[ab]', '', regex=False)
print(result)
ATypeError: cannot use regex pattern when regex=False
BValueError: regex argument must be True or False
CTypeError: expected string or bytes-like object
D
No error, output is:
0    ct
1    bt
2    rt
dtype: object
Attempts:
2 left
💡 Hint
Check the meaning of regex=False when the pattern looks like a regex.
visualization
advanced
2:00remaining
Visualize the count of string lengths in a Series
Which option shows the correct bar plot code to visualize the count of string lengths in this Series?
import pandas as pd
import matplotlib.pyplot as plt
s = pd.Series(['dog', 'cat', 'elephant', 'fox'])
lengths = s.str.len()
# Which code below plots counts of each length?
Data Analysis Python
import pandas as pd
import matplotlib.pyplot as plt
s = pd.Series(['dog', 'cat', 'elephant', 'fox'])
lengths = s.str.len()
A
lengths.value_counts().sort_index().plot(kind='bar')
plt.show()
B
lengths.plot(kind='bar')
plt.show()
C
s.str.len().plot(kind='line')
plt.show()
D
lengths.hist()
plt.show()
Attempts:
2 left
💡 Hint
We want counts of each length, not the lengths themselves.
🚀 Application
expert
3:00remaining
Filter Series for strings starting and ending with vowels
Given this Series, which option correctly filters strings that start and end with a vowel?
import pandas as pd
s = pd.Series(['apple', 'banana', 'orange', 'umbrella', 'grape', 'avocado'])
Data Analysis Python
import pandas as pd
s = pd.Series(['apple', 'banana', 'orange', 'umbrella', 'grape', 'avocado'])
A
filtered = s[s.str.match('^[aeiou].*[aeiou]$')]
print(filtered)
B
filtered = s[s.str.contains('^[aeiou].*[aeiou]$')]
print(filtered)
C
filtered = s[s.str.startswith(('a','e','i','o','u')) & s.str.endswith(('a','e','i','o','u'))]
print(filtered)
D
filtered = s[s.str.match('^[aeiou].*[aeiou]')]
print(filtered)
Attempts:
2 left
💡 Hint
Check how to combine startswith and endswith for multiple letters.