Types and Parsing
Parse Formatted Numbers
Convert number strings that contain formatting characters (commas as
thousands separators) to integers. By hand, call str.replace(',', '')
to strip the commas before passing to int. With pandas, chain
Series.str.replace(',', '') and .astype(int) to do the same
column-wide in two steps.
By hand
Loop over strings, call .replace(',', '') on each to produce a clean
digit-only string, then pass to int(). The trace shows result growing
with the parsed integers.
naive.py
Replay: real traced execution (multi-file project)
strings = ['1,200', '3,450', '850', '12,000', '675']
result = []
for s in strings:
result.append(int(s.replace(',', '')))
print('RESULT:', result)
strings ← ['1,200', '3,450', '850', '12,000', '675']
1strings = ['1,200', '3,450', '850', '12,000', '675']2result = []values this step['1,200', '3,450', '850', '12,000', '675']stringsresult ← []
1strings = ['1,200', '3,450', '850', '12,000', '675']2result = []3for s in strings:values this step[]results ← '1,200'
2result = []3for s in strings:4 result.append(int(s.replace(',', '')))values this step'1,200'sresult ← [1200]
3for s in strings:4 result.append(int(s.replace(',', '')))5print('RESULT:', result)values this step[] → [1200]results ← '3,450'
2result = []3for s in strings:4 result.append(int(s.replace(',', '')))values this step'1,200' → '3,450'sresult ← [1200, 3450]
3for s in strings:4 result.append(int(s.replace(',', '')))5print('RESULT:', result)values this step[1200] → [1200, 3450]results ← '850'
2result = []3for s in strings:4 result.append(int(s.replace(',', '')))values this step'3,450' → '850'sresult ← [1200, 3450, 850]
3for s in strings:4 result.append(int(s.replace(',', '')))5print('RESULT:', result)values this step[1200, 3450] → [1200, 3450, 850]results ← '12,000'
2result = []3for s in strings:4 result.append(int(s.replace(',', '')))values this step'850' → '12,000'sresult ← [1200, 3450, 850, 12000]
3for s in strings:4 result.append(int(s.replace(',', '')))5print('RESULT:', result)values this step[1200, 3450, 850] → [1200, 3450, 850, 12000]results ← '675'
2result = []3for s in strings:4 result.append(int(s.replace(',', '')))values this step'12,000' → '675'sresult ← [1200, 3450, 850, 12000, 675]
3for s in strings:4 result.append(int(s.replace(',', '')))5print('RESULT:', result)values this step[1200, 3450, 850, 12000] → [1200, 3450, 850, 12000, 675]resultfor s in strings:
2result = []3for s in strings:4 result.append(int(s.replace(',', '')))stdout ← RESULT: [1200, 3450, 850, 12000, 675]
4 result.append(int(s.replace(',', '')))5print('RESULT:', result)values this stepRESULT: [1200, 3450, 850, 12000, 675]stdout
With pandas
.str.replace(',', '') strips every comma from the string column (dtype
stays object). Chaining .astype(int) then converts the cleaned strings
to int64. The snapshot shows the dtype change from object to int64,
matching cast-numeric-strings.
library.py
import pandas as pd
from dalib.display import set_display
set_display()
strings = ['1,200', '3,450', '850', '12,000', '675']
df = pd.DataFrame({'x': strings})
s = df['x'].str.replace(',', '').astype(int)
result = s.tolist()
print('index:', s.index.tolist())
print('dtype before:', df['x'].dtype)
print('dtype after:', s.dtype)
print('RESULT:', result)
index: [0, 1, 2, 3, 4]
dtype before: object
dtype after: int64
RESULT: [1200, 3450, 850, 12000, 675]
Implementation notes
.str.replacedefaults toregex=False(literal replacement). Passregex=Trueonly when you intend a regex pattern — e.g. to strip multiple characters at once with a character class.- For currency strings like
'$1,200.50', chain.str.replace('[$,]', '', regex=True)then.astype(float). - Cross-reference:
cast-numeric-strings(this chapter) for converting already-clean digit strings to integers without pre-processing.