DataFrame attributes (shape/dtypes/columns)
Attributes tell you the size, column names and data types of a DataFrame without printing all of it.
In this page:
Syntax
df.shape
df.dtypes
df.columns
df.index
DataFrame attributes (shape/dtypes/columns)
shape gives (rows, columns), columns lists the column labels, index shows row labels, and dtypes reports each column's type. These are attributes, not methods, so no parentheses. values returns the underlying NumPy array.
Note:
len(df) returns the number of rows.
Example: DataFrame attributes (shape/dtypes/columns)
import pandas as pd
df = pd.DataFrame({"name": ["Ann", "Bob"], "age": [28, 35], "score": [88.5, 92.0]})
print(df.shape)
print(list(df.columns))
print(df.dtypes)
print(len(df))
# Output:
# (2, 3)
# ['name', 'age', 'score']
# name object
# age int64
# score float64
# dtype: object
# 2
Related Topics
Common Mistakes
- Calling shape() as a function
- Forgetting dtypes is plural
- Confusing len(df) with the number of columns
Chapter Summary
- shape is (rows, cols)
- columns lists the labels
- dtypes shows types
- They are attributes not methods
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: