0
0
Pandasdata~10 mins

str.lower() and str.upper() in Pandas - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - str.lower() and str.upper()
Start with a pandas Series
Apply str.lower()
Convert all text to lowercase
Output new Series with lowercase strings
Apply str.upper()
Convert all text to uppercase
Output new Series with uppercase strings
This flow shows how to take a pandas Series of text and convert all strings to lowercase or uppercase using str.lower() and str.upper().
Execution Sample
Pandas
import pandas as pd
s = pd.Series(['Apple', 'Banana', 'Cherry'])
lower_s = s.str.lower()
upper_s = s.str.upper()
This code creates a Series of fruit names, then makes new Series with all lowercase and all uppercase letters.
Execution Table
StepActionInput SeriesMethod AppliedOutput Series
1Create Series['Apple', 'Banana', 'Cherry']None['Apple', 'Banana', 'Cherry']
2Apply str.lower()['Apple', 'Banana', 'Cherry']str.lower()['apple', 'banana', 'cherry']
3Apply str.upper()['Apple', 'Banana', 'Cherry']str.upper()['APPLE', 'BANANA', 'CHERRY']
4End---
💡 All strings converted; no more steps.
Variable Tracker
VariableStartAfter str.lower()After str.upper()
s['Apple', 'Banana', 'Cherry']['Apple', 'Banana', 'Cherry']['Apple', 'Banana', 'Cherry']
lower_sN/A['apple', 'banana', 'cherry']['apple', 'banana', 'cherry']
upper_sN/AN/A['APPLE', 'BANANA', 'CHERRY']
Key Moments - 2 Insights
Why does the original Series 's' not change after applying str.lower() or str.upper()?
Because str.lower() and str.upper() return new Series objects with the changes; they do not modify the original Series 's'. See execution_table rows 2 and 3 where input remains the same.
What happens if the Series contains non-string values?
Non-string values are ignored or result in NaN in the output Series when using str.lower() or str.upper(). This example only has strings, so all convert successfully.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the output Series?
A['APPLE', 'BANANA', 'CHERRY']
B['Apple', 'Banana', 'Cherry']
C['apple', 'banana', 'cherry']
D['aPPLE', 'bANANA', 'cHERRY']
💡 Hint
Check the 'Output Series' column at step 2 in the execution_table.
At which step does the Series get converted to uppercase?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Method Applied' column to find where str.upper() is used.
If we change the original Series to include numbers, what will happen when applying str.lower()?
ANumbers will become NaN in the output
BNumbers will convert to lowercase letters
CNumbers will cause an error
DNumbers will remain unchanged
💡 Hint
Recall that str methods work on strings; non-strings become NaN as explained in key_moments.
Concept Snapshot
pandas Series.str.lower() and str.upper()
- Convert all string elements to lowercase or uppercase
- Returns a new Series; original stays unchanged
- Non-string values become NaN
- Usage: new_series = series.str.lower() or series.str.upper()
Full Transcript
We start with a pandas Series containing strings. Using str.lower() converts all strings to lowercase and returns a new Series without changing the original. Similarly, str.upper() converts all strings to uppercase. Non-string values would become NaN if present. This process helps standardize text data for analysis.