str.lower() and str.upper() in Pandas - Time & Space Complexity
We want to understand how long it takes to change text to all lowercase or uppercase in pandas.
How does the time needed grow when we have more text data?
Analyze the time complexity of the following code snippet.
import pandas as pd
# Create a DataFrame with a column of strings
df = pd.DataFrame({
'names': ['Alice', 'Bob', 'Charlie', 'David', 'Eva'] * 1000
})
# Convert all names to lowercase
lower_names = df['names'].str.lower()
# Convert all names to uppercase
upper_names = df['names'].str.upper()
This code changes all the strings in the 'names' column to lowercase and uppercase.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Applying the lower() or upper() function to each string in the column.
- How many times: Once for each string in the column (n times, where n is the number of rows).
Each string is processed one by one, so if we have more strings, the work grows directly with that number.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 string conversions |
| 100 | 100 string conversions |
| 1000 | 1000 string conversions |
Pattern observation: The time grows in a straight line as the number of strings increases.
Time Complexity: O(n)
This means the time to convert all strings grows directly with how many strings there are.
[X] Wrong: "Changing case is instant and does not depend on data size."
[OK] Correct: Each string must be checked and changed, so more strings mean more work and more time.
Understanding how string operations scale helps you explain your code choices clearly and shows you know how data size affects performance.
"What if we changed from using str.lower() on a column to applying a custom function that also changes case and adds extra text? How would the time complexity change?"
Practice
str.lower() do when applied to a Series of text data?Solution
Step 1: Understand the method purpose
Thestr.lower()method changes all letters in text to lowercase.Step 2: Compare with other options
Other options describe different actions like uppercase, removing spaces, or reversing text, which are not whatstr.lower()does.Final Answer:
Converts all characters in the text to lowercase. -> Option AQuick Check:
str.lower() = lowercase conversion [OK]
- Confusing str.lower() with str.upper()
- Thinking it removes spaces
- Assuming it reverses text
names to uppercase?Solution
Step 1: Recall pandas string method syntax
To apply string methods on a pandas Series, useSeries.str.method()syntax.Step 2: Check each option
names.str.upper() usesnames.str.upper(), which is correct. names.upper() misses.str, str.upper(names) is incorrect function call, names.str.upper misses parentheses.Final Answer:
names.str.upper() -> Option AQuick Check:
Use .str.upper() on Series [OK]
- Calling upper() directly on Series without .str
- Forgetting parentheses after method
- Using str.upper() as a standalone function
data = pd.Series(['Apple', 'Banana', 'Cherry']), what is the output of data.str.lower()?Solution
Step 1: Understand the input Series
The Series contains capitalized fruit names: 'Apple', 'Banana', 'Cherry'.Step 2: Apply str.lower() method
This method converts all letters to lowercase, so each word becomes fully lowercase.Final Answer:
['apple', 'banana', 'cherry'] -> Option CQuick Check:
str.lower() makes all letters lowercase [OK]
- Expecting original case to remain
- Confusing with str.upper() output
- Thinking only first letter changes
cities to uppercase?cities = pd.Series(['Paris', 'London', 'Berlin']) cities_upper = cities.upper()
Solution
Step 1: Identify method usage on Series
String methods like upper() must be called with .str accessor on pandas Series.Step 2: Check the code
The code uses cities.upper() which is invalid; correct is cities.str.upper().Final Answer:
Method upper() should be called as cities.str.upper(). -> Option DQuick Check:
Use .str.upper() on Series [OK]
- Calling upper() directly on Series
- Forgetting .str accessor
- Removing parentheses from method call
df with a column 'Product' containing mixed case product names. You want to create a new column 'Product_Upper' with all names in uppercase. Which code correctly does this?Solution
Step 1: Understand the DataFrame and goal
The goal is to create a new column with uppercase product names from an existing column.Step 2: Apply correct method for uppercase conversion
Use.str.upper()on the Series to convert text to uppercase correctly.Step 3: Check options
df['Product_Upper'] = df['Product'].str.upper() uses correct syntax. df['Product_Upper'] = df['Product'].upper() misses .str, df['Product_Upper'] = str.upper(df['Product']) uses wrong function call, df['Product_Upper'] = df['Product'].str.lower() converts to lowercase, not uppercase.Final Answer:
df['Product_Upper'] = df['Product'].str.upper() -> Option BQuick Check:
Use .str.upper() to uppercase Series text [OK]
- Calling upper() without .str
- Using str.upper() as a function on Series
- Confusing upper() with lower()
Ready to prove it?
Pass the test and this pattern moves unknown
