Convert a list of clean numeric strings to integers. By hand, loop and call int(s) on each element. With pandas, Series.astype(int) converts the entire column in one call, changing the dtype from object to int64.

By hand

Loop over strings and call int(s) to convert each element, appending to result. Every iteration shows s being consumed and the growing list of integers.

naive.py
Replay: real traced execution (multi-file project)
strings = ['12', '7', '45', '3', '28']
result = []
for s in strings:
    result.append(int(s))
print('RESULT:', result)
  1. strings ← ['12', '7', '45', '3', '28']

    1strings = ['12', '7', '45', '3', '28']2result = []
    values this step['12', '7', '45', '3', '28']strings
  2. result ← []

    1strings = ['12', '7', '45', '3', '28']2result = []3for s in strings:
    values this step[]result
  3. s ← '12'

    2result = []3for s in strings:4    result.append(int(s))
    values this step'12's
  4. result ← [12]

    3for s in strings:4    result.append(int(s))5print('RESULT:', result)
    values this step[] [12]result
  5. s ← '7'

    2result = []3for s in strings:4    result.append(int(s))
    values this step'12' '7's
  6. result ← [12, 7]

    3for s in strings:4    result.append(int(s))5print('RESULT:', result)
    values this step[12] [12, 7]result
  7. s ← '45'

    2result = []3for s in strings:4    result.append(int(s))
    values this step'7' '45's
  8. result ← [12, 7, 45]

    3for s in strings:4    result.append(int(s))5print('RESULT:', result)
    values this step[12, 7] [12, 7, 45]result
  9. s ← '3'

    2result = []3for s in strings:4    result.append(int(s))
    values this step'45' '3's
  10. result ← [12, 7, 45, 3]

    3for s in strings:4    result.append(int(s))5print('RESULT:', result)
    values this step[12, 7, 45] [12, 7, 45, 3]result
  11. s ← '28'

    2result = []3for s in strings:4    result.append(int(s))
    values this step'3' '28's
  12. result ← [12, 7, 45, 3, 28]

    3for s in strings:4    result.append(int(s))5print('RESULT:', result)
    values this step[12, 7, 45, 3] [12, 7, 45, 3, 28]result
  13. for s in strings:

    2result = []3for s in strings:4    result.append(int(s))
  14. stdout ← RESULT: [12, 7, 45, 3, 28]

    4    result.append(int(s))5print('RESULT:', result)
    values this stepRESULT: [12, 7, 45, 3, 28]stdout

With pandas

df['x'] starts as dtype object (string column). .astype(int) returns a new int64 Series. The snapshot shows both dtypes so the conversion is explicit.

library.py
import pandas as pd
from dalib.display import set_display
set_display()

strings = ['12', '7', '45', '3', '28']
df = pd.DataFrame({'x': strings})
s = df['x'].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: [12, 7, 45, 3, 28]

Implementation notes

  • astype(int) raises ValueError on any non-numeric string — it is a strict cast, not a coercion. Use it only when all values are known to be clean.
  • To cast to float instead, use astype(float). For large datasets where memory matters, specify a smaller dtype: astype('int32') or astype('int16').
  • Cross-reference: coerce-bad-numbers (this chapter) for handling strings that may fail conversion without raising.