DataFrame Anatomy & Attributes
The parts of a DataFrame — index, columns, values, axes — and the attributes that describe it: shape, size, ndim, dtypes, to_numpy and more.
Watch the lesson
1 video on CodeWithMunnaX
Before analysing any dataset, you should be able to answer: How many rows and columns? What are the column names? What type is each column? What are the row labels? DataFrame attributes answer all of these instantly. They also teach you the vocabulary — index, columns, axis 0, axis 1 — that every pandas method uses.
We will use this IPL 2023 top run-scorers table throughout (sample data):
text
player team matches runs strike_rate
0 Shubman Gill GT 17 890 157.8
1 Faf du Plessis RCB 14 730 153.7
2 Devon Conway CSK 16 672 139.7
3 Virat Kohli RCB 14 639 139.8
4 Yashasvi Jaiswal RR 14 625 163.6
5 Suryakumar Yadav MI 16 605 181.1
6 Ruturaj Gaikwad CSK 16 590 147.5
7 Rinku Singh KKR 14 474 149.5Definition
The anatomy of a DataFrame has three parts: the index (row labels, along axis 0), the columns (column labels, along axis 1), and the values (the 2D grid of data). Attributes such as
shape,dtypesandcolumnsdescribe these parts without changing them.
text
columns (axis 1 →)
┌──────────────────────────────────────────┐
│ player team matches runs ... │
index ┌───┼──────────────────────────────────────────┤
(axis 0 │ 0 │ Shubman Gill GT 17 890 │
↓) │ 1 │ Faf du Plessis RCB 14 730 values │
│ 2 │ Devon Conway CSK 16 672 │
└───┴──────────────────────────────────────────┘- axis 0 = rows (down), axis 1 = columns (across) — the same convention as NumPy. Methods like
drop,sumandmeantakeaxis=to say which way to work. - Index — the row labels. A default index is a memory-efficient
RangeIndex. - Columns — also an
Indexobject, holding the column names.
Syntax
| Attribute | Returns | Meaning |
|---|---|---|
df.shape | tuple (rows, cols) | number of rows and columns |
df.size | int | total cells = rows × cols (includes NaN) |
df.ndim | int | always 2 |
len(df) | int | number of rows |
df.index | Index | the row labels |
df.columns | Index | the column names |
df.dtypes | Series | the dtype of every column |
df.values / df.to_numpy() | ndarray | the data as a NumPy array |
df.axes | list | [df.index, df.columns] |
df.empty | bool | True if there are no rows or no columns |
df.T | DataFrame | transpose — rows become columns |
Example — shape, size, ndim, index and columns
python
print(df.index)
print(df.columns)
print(df.shape)
print(df.size)
print(df.ndim)
print(len(df))text
RangeIndex(start=0, stop=8, step=1)
Index(['player', 'team', 'matches', 'runs', 'strike_rate'], dtype='str')
(8, 5)
40
2
8How it works: shape is (8, 5) → 8 rows, 5 columns; use df.shape[0] for the row count and df.shape[1] for the column count. size = 8 × 5 = 40 cells. The index is a RangeIndex from 0 up to (not including) 8. Column names are stored in an Index with dtype str. Use df.columns.tolist() to get a plain list.
Example — dtypes
python
print(df.dtypes)text
player str
team str
matches int64
runs int64
strike_rate float64
dtype: objectHow it works: dtypes returns a Series — index = column names, values = their dtypes. (The footer dtype: object is the dtype of that Series of dtypes; ignore it.) This is the first place to look when maths on a column fails — numbers stored as str cannot be summed.
Note (pandas 3): text columns show as
str. Older tutorials showobjectfor the same data.
Example — values / to_numpy and axes
python
print(df[["matches", "runs"]].to_numpy())
print(df.to_numpy()[:2])
print(df.axes)text
[[ 17 890]
[ 14 730]
[ 16 672]
[ 14 639]
[ 14 625]
[ 16 605]
[ 16 590]
[ 14 474]]
[['Shubman Gill' 'GT' 17 890 157.8]
['Faf du Plessis' 'RCB' 14 730 153.7]]
[RangeIndex(start=0, stop=8, step=1), Index(['player', 'team', 'matches', 'runs', 'strike_rate'], dtype='str')]How it works: to_numpy() drops the labels and returns the raw 2D array. For two integer columns the array is int64. For the whole table — text and numbers mixed — NumPy needs one common type, so the array becomes object (generic Python objects). That is why machine-learning code usually selects numeric columns first. df.values does the same thing, but to_numpy() is the recommended, explicit name.
Example — transpose
python
print(df.T.iloc[:, :3])text
0 1 2
player Shubman Gill Faf du Plessis Devon Conway
team GT RCB CSK
matches 17 14 16
runs 890 730 672
strike_rate 157.8 153.7 139.7df.T swaps rows and columns — handy for reading one wide record vertically.
Comparison — Series vs DataFrame attributes
| Attribute | Series | DataFrame |
|---|---|---|
shape | (n,) | (rows, cols) |
ndim | 1 | 2 |
size | n | rows × cols |
len() | n | rows |
| type info | .dtype (one) | .dtypes (per column) |
| labels | .index | .index and .columns |
.name | ✅ | ❌ |
Common mistakes
- ❌
df.shape()→TypeError: 'tuple' object is not callable. ✅df.shape(no parentheses). - ❌
df.dtype→AttributeError. ✅df.dtypes(plural) for a DataFrame. - ❌ Using
df.sizeto count rows. ✅len(df)ordf.shape[0];sizecounts cells. - ❌ Expecting
df.to_numpy()on a mixed table to be numeric. ✅ Select numeric columns first (df.select_dtypes("number").to_numpy()).
Interview tip
Q: What is the difference between len(df), df.shape and df.size?
A: len(df) is the number of rows; df.shape is the tuple (rows, columns); df.size is the total number of cells, rows × columns, including missing values.
Key points to remember
- A DataFrame has an index (row labels, axis 0), columns (column labels, axis 1) and values.
- df.shape is (rows, columns); len(df) is the row count; df.size is rows × columns.
- df.ndim is always 2; df.axes is [df.index, df.columns].
- df.dtypes is a Series listing each column's dtype — text shows as str in pandas 3.
- df.to_numpy() returns the raw 2D array; a mixed table becomes an object array.
- df.columns.tolist() gives the column names as a plain list.
- df.T transposes rows and columns.
Try it yourself
Anatomy of an IPL table
dtypes and to_numpy
Practice: describe your table in one line
Revision Flashcards
Tap a card to flip it and see the answer.
