Column data types
Every column has a dtype, and converting the right dtype saves memory and prevents wrong results.
In this page:
Syntax
df.dtypes
df["column"].dtype
Column data types
dtypes shows each column's type: int64, float64, object (text), bool, datetime64 and category. astype converts, and pd.to_numeric or pd.to_datetime convert with more control. Correct dtypes make sorting, maths and memory use behave properly.
Note:
select_dtypes(include="number") picks only numeric columns.
Example: Column data types
import pandas as pd
df = pd.DataFrame({"id": ["1", "2", "3"], "price": ["9.5", "3.25", "7"]})
print(df.dtypes)
df["id"] = df["id"].astype(int)
df["price"] = pd.to_numeric(df["price"])
print(df.dtypes)
print(df.select_dtypes(include="number").columns.tolist())
# Output:
# id object
# price object
# dtype: object
# id int64
# price float64
# dtype: object
# ['id', 'price']
Related Topics
Common Mistakes
- Leaving numbers stored as text
- Converting text with commas without cleaning
- Forgetting astype returns a new object
Chapter Summary
- dtypes lists column types
- astype converts
- to_numeric and to_datetime add control
- select_dtypes filters by type
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: