Given the following DataFrame, what will be the output of df['name'].str.upper()?
import pandas as pd df = pd.DataFrame({'name': ['alice', 'Bob', 'CHARLIE']}) result = df['name'].str.upper() print(result)
The str.upper() method converts all characters in the string to uppercase.
The str.upper() method changes all letters to uppercase regardless of their original case.
Using the DataFrame below, how many rows have the substring 'cat' in the animal column?
import pandas as pd df = pd.DataFrame({'animal': ['cat', 'dog', 'caterpillar', 'bird', 'concatenate']}) count = df['animal'].str.contains('cat').sum() print(count)
Check which strings include the exact substring 'cat' anywhere inside.
The strings 'cat', 'caterpillar', and 'concatenate' all contain 'cat', so the count is 3.
Consider this code snippet:
import pandas as pd
df = pd.DataFrame({'text': ['apple', 'banana', None]})
df['text'].str.len()What error or output occurs?
import pandas as pd df = pd.DataFrame({'text': ['apple', 'banana', None]}) result = df['text'].str.len() print(result)
pandas string methods handle None values gracefully by returning NaN.
The str.len() method returns the length of each string. For None values, it returns NaN instead of raising an error.
Given this DataFrame, which plot correctly shows the count of each string length in the words column?
import pandas as pd import matplotlib.pyplot as plt df = pd.DataFrame({'words': ['cat', 'dog', 'bird', 'fish', 'elephant']}) lengths = df['words'].str.len() counts = lengths.value_counts().sort_index() counts.plot(kind='bar') plt.show()
Count how many words have length 3, 4, and 8.
Words 'cat' and 'dog' have length 3 (2 counts), 'bird' and 'fish' length 4 (2 counts), 'elephant' length 8 (1 count).
Which reason best explains why using pandas string dtype is better than object dtype for text columns?
Think about missing data handling and performance.
The pandas string dtype is designed for text data with native missing value support and optimized methods, unlike object dtype which is a generic container.