String Cleaning
Normalize Case
Fold a column of mixed-case strings to a consistent case for matching and
grouping. By hand, call s.lower() on each element. With pandas,
Series.str.lower() applies the lowercasing across the entire column.
By hand
Loop over names and call .lower() on each string, appending to
result. The trace shows each name reduced to lowercase one at a time.
naive.py
Replay: real traced execution (multi-file project)
names = ['Alice', 'BOB', 'carol', 'DAVE', 'Eve']
result = []
for s in names:
result.append(s.lower())
print('RESULT:', result)
names ← ['Alice', 'BOB', 'carol', 'DAVE', 'Eve']
1names = ['Alice', 'BOB', 'carol', 'DAVE', 'Eve']2result = []values this step['Alice', 'BOB', 'carol', 'DAVE', 'Eve']namesresult ← []
1names = ['Alice', 'BOB', 'carol', 'DAVE', 'Eve']2result = []3for s in names:values this step[]results ← 'Alice'
2result = []3for s in names:4 result.append(s.lower())values this step'Alice'sresult ← ['alice']
3for s in names:4 result.append(s.lower())5print('RESULT:', result)values this step[] → ['alice']results ← 'BOB'
2result = []3for s in names:4 result.append(s.lower())values this step'Alice' → 'BOB'sresult ← ['alice', 'bob']
3for s in names:4 result.append(s.lower())5print('RESULT:', result)values this step['alice'] → ['alice', 'bob']results ← 'carol'
2result = []3for s in names:4 result.append(s.lower())values this step'BOB' → 'carol'sresult ← ['alice', 'bob', 'carol']
3for s in names:4 result.append(s.lower())5print('RESULT:', result)values this step['alice', 'bob'] → ['alice', 'bob', 'carol']results ← 'DAVE'
2result = []3for s in names:4 result.append(s.lower())values this step'carol' → 'DAVE'sresult ← ['alice', 'bob', 'carol', 'dave']
3for s in names:4 result.append(s.lower())5print('RESULT:', result)values this step['alice', 'bob', 'carol'] → ['alice', 'bob', 'carol', 'dave']results ← 'Eve'
2result = []3for s in names:4 result.append(s.lower())values this step'DAVE' → 'Eve'sresult ← ['alice', 'bob', 'carol', 'dave', 'eve']
3for s in names:4 result.append(s.lower())5print('RESULT:', result)values this step['alice', 'bob', 'carol', 'dave'] → ['alice', 'bob', 'carol', 'dave', 'eve']resultfor s in names:
2result = []3for s in names:4 result.append(s.lower())stdout ← RESULT: ['alice', 'bob', 'carol', 'dave', 'eve']
4 result.append(s.lower())5print('RESULT:', result)values this stepRESULT: ['alice', 'bob', 'carol', 'dave', 'eve']stdout
With pandas
df['x'].str.lower() returns a new object Series with every string
lowercased. The dtype is unchanged. The snapshot confirms all values are
now lowercase.
library.py
import pandas as pd
from dalib.display import set_display
set_display()
names = ['Alice', 'BOB', 'carol', 'DAVE', 'Eve']
df = pd.DataFrame({'x': names})
s = df['x'].str.lower()
result = s.tolist()
print('index:', s.index.tolist())
print('dtype:', s.dtype)
print('values:', s.tolist())
print('RESULT:', result)
index: [0, 1, 2, 3, 4]
dtype: object
values: ['alice', 'bob', 'carol', 'dave', 'eve']
RESULT: ['alice', 'bob', 'carol', 'dave', 'eve']
Implementation notes
.lower()is the most common normalization for lookups and groupby keys. Use.upper()for uppercase or.title()for Title Case (first letter of each word capitalized).- Case normalization is usually applied after stripping whitespace — a
leading space survives
.lower()unchanged. - Cross-reference:
strip-whitespace(this chapter) — the two are typically chained as the opening cleaning steps. - Cross-reference:
normalize-text-fields(python-data-basics) for the pure-Python pattern that combines strip and lowercase in one pass.