astype
df["col"].astype(int) converts a column to the given type (int, float, str, "category", "Int64" and so on) and raises if a value cannot be converted. For messy text that may contain bad values, pd.to_numeric(col, errors="coerce") turns the unconvertible ones into NaN.
Syntax
Series.astype(dtype, errors="raise") | df.astype({"a": int, "b": str}) | pd.to_numeric(series, errors="coerce")| Part | What it is |
|---|---|
dtype | int, float, str, bool, "category", "Int64", "datetime64[ns]" or a numpy dtype |
errors | "raise" (default) or "ignore" to return the original on failure |
Examples
import pandas as pd
df = pd.DataFrame({"qty": ["3", "10", "7"], "price": [2.5, 4.0, 1.25]})
df["qty"] = df["qty"].astype(int)
df.dtypesResult: qty int64 price float64 dtype: object
s = pd.Series(["12", "n/a", "7"])
pd.to_numeric(s, errors="coerce")Result: 0 12.0 1 NaN 2 7.0 dtype: float64
df["price"].astype(int)Result: 0 2 1 4 2 1 Name: price, dtype: int64
astype(int) truncates toward zero; use .round().astype(int) to round.
Common mistakes
- ValueError: invalid literal for int() with base 10: '1,200': strip the thousands separator first, s.str.replace(",", "").astype(int).
- IntCastingNaNError: Cannot convert non-finite values: the column has NaN; fill it or use the nullable "Int64" dtype.
- astype(str) turns NaN into the text "nan" which no longer counts as missing; handle NaN before converting.
Practice astype
These drills check your answer on live data, free, no account needed.
- Convert text to numbersThe `price` column was read in as text. Convert it to float, add a `total` column equal to `qty * price`, and return the whole `items` table.
- Pull numbers out of codesEach code looks like ORD-2024-0153. Extract the final group of digits as an integer (so 0153 becomes 153) and return it as a Series.
Questions
How do I convert several columns at once?
Pass a dict: df = df.astype({"qty": int, "zip": str}). For many numeric columns, df[cols] = df[cols].apply(pd.to_numeric, errors="coerce").
When should I use category dtype?
For text columns with few distinct values repeated many times (country, status). It cuts memory and speeds up groupby and sorting.
How do I convert a column to datetime?
Use pd.to_datetime(df["col"]) rather than astype; it parses many formats and has errors="coerce".
Related
- pd.to_datetimeHow to parse dates in pandas with pd.to_datetime: formats, day-first dates, coercing bad values, and extracting year, month and weekday with the dt accessor.
- fillnaHow to fill NaN values in pandas with fillna: a constant, a different value per column, the column mean or median, forward and backward fill, and by group.
- pd.read_csvHow to read a CSV file in pandas with pd.read_csv: separators, headers, index columns, parsing dates, selecting columns, dtypes and the errors people hit.
- str.splitHow to split a string column in pandas with str.split: into separate columns with expand=True, limit the number of splits, take one part with str[0], regex.