← Back to Pandas Course | Chapter 3: DataFrame Basics | Lesson 7 of 7

Column data types

Every column has a dtype, and converting the right dtype saves memory and prevents wrong results.

In this page:

  1. Column data types
Syntax
python
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

python
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
  1. Leaving numbers stored as text
  2. Converting text with commas without cleaning
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.