0
0
Pandasdata~20 mins

str.strip() for whitespace in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Whitespace Stripping Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of str.strip() on pandas Series with whitespace
What is the output of the following code?
import pandas as pd
s = pd.Series(['  apple ', ' banana', 'cherry  ', '  date  '])
result = s.str.strip()
print(result.tolist())
Pandas
import pandas as pd
s = pd.Series(['  apple ', ' banana', 'cherry  ', '  date  '])
result = s.str.strip()
print(result.tolist())
A[' apple', 'banana', 'cherry ', 'date']
B['apple', 'banana', 'cherry', 'date']
C['apple ', ' banana', 'cherry', 'date ']
D[' apple ', ' banana', 'cherry ', ' date ']
Attempts:
2 left
💡 Hint
str.strip() removes spaces from both ends of each string in the Series.
data_output
intermediate
2:00remaining
Count of strings after stripping whitespace
After stripping whitespace from the Series below, how many strings start with the letter 'b'?
import pandas as pd
s = pd.Series(['  apple ', ' banana', 'blueberry  ', '  date  ', '  berry'])
stripped = s.str.strip()
count_b = stripped.str.startswith('b').sum()
print(count_b)
Pandas
import pandas as pd
s = pd.Series(['  apple ', ' banana', 'blueberry  ', '  date  ', '  berry'])
stripped = s.str.strip()
count_b = stripped.str.startswith('b').sum()
print(count_b)
A3
B2
C1
D4
Attempts:
2 left
💡 Hint
Check which strings start with 'b' after removing spaces.
🔧 Debug
advanced
2:00remaining
Identify the error in using str.strip() on a non-string column
What error will this code raise?
import pandas as pd
df = pd.DataFrame({'numbers': [1, 2, 3, 4]})
df['clean'] = df['numbers'].str.strip()
Pandas
import pandas as pd
df = pd.DataFrame({'numbers': [1, 2, 3, 4]})
df['clean'] = df['numbers'].str.strip()
AKeyError: 'str'
BNo error, returns original numbers
CTypeError: strip() argument must be str, not int
DAttributeError: Can only use .str accessor with string values!
Attempts:
2 left
💡 Hint
The .str accessor only works on string data.
🚀 Application
advanced
2:00remaining
Using str.strip() to clean a DataFrame column before analysis
You have a DataFrame with a column 'city' containing city names with extra spaces. Which code snippet correctly cleans the 'city' column by removing leading and trailing spaces?
import pandas as pd
df = pd.DataFrame({'city': [' New York ', 'Los Angeles ', ' Chicago', 'Houston ']})
Pandas
import pandas as pd
df = pd.DataFrame({'city': [' New York ', 'Los Angeles ', ' Chicago', 'Houston ']})
Adf['city'] = df['city'].apply(strip())
Bdf['city'] = df['city'].strip()
Cdf['city'] = df['city'].str.strip()
Ddf['city'] = df['city'].map(str.strip)
Attempts:
2 left
💡 Hint
Use the pandas string accessor for element-wise string operations.
🧠 Conceptual
expert
2:00remaining
Understanding str.strip() behavior with custom characters
What will be the output of this code?
import pandas as pd
s = pd.Series(['xxapplexx', 'xbanana', 'cherryxx', 'xxdate'])
result = s.str.strip('x')
print(result.tolist())
Pandas
import pandas as pd
s = pd.Series(['xxapplexx', 'xbanana', 'cherryxx', 'xxdate'])
result = s.str.strip('x')
print(result.tolist())
A['apple', 'banana', 'cherry', 'date']
B['xxapplexx', 'xbanana', 'cherryxx', 'xxdate']
C['applexx', 'banana', 'cherry', 'date']
D['apple', 'banana', 'cherryxx', 'date']
Attempts:
2 left
💡 Hint
str.strip(chars) removes all characters in 'chars' from both ends until a different character is found.