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

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.

naive.py
strings = ['12', '7', '45', '3', '28']
result = []
for s in strings:
    result.append(int(s))
print('RESULT:', result)
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.