Sub topic 1
A DataFrame is typed columns sharing an index
Prerequisites
This chapter assumes you can create a DataFrame and select columns with df["name"]. You should know the basic Python types int, float, and str. Familiarity with NumPy arrays helps but is not required.
The working model
A pandas DataFrame is not a 2D grid of mixed values. It is a set of columns, and each column is a Series. All the columns share one row index, which labels the rows. Each column holds exactly one data type, called its dtype.
This model explains many behaviors that otherwise look odd:
- Operations on a column are fast because the values sit in one array of one type.
- Two columns in the same frame can have different dtypes.
- A single column cannot mix, say, integers and floats without pandas choosing one common dtype for the whole column.
Inspecting column dtypes
The dtypes attribute returns one dtype per column.
import pandas as pd
df = pd.DataFrame({
"a": [1, 2, 3],
"b": [1.5, 2.5, 3.5],
"c": ["x", "y", "z"],
})
print(df.dtypes)The output is:
a int64
b float64
c object
dtype: objectColumn a is int64 and column b is float64. Column c holds strings and appears as object in pandas 2.x. The object dtype is covered in a later chapter. For now, treat it as the dtype pandas uses when no more specific one fits.
The last line, dtype: object, describes the result of df.dtypes itself. It is a Series whose values are dtype objects, so its own dtype is object.
Columns share the index, not the type
The index is the common thread. When you select a column, you get a Series that carries the same index as the frame. When you combine columns in arithmetic, pandas aligns them by index labels first and then operates on the values.
import pandas as pd
df = pd.DataFrame(
{"a": [1, 2, 3], "b": [1.5, 2.5, 3.5]},
index=["r1", "r2", "r3"],
)
total = df["a"] + df["b"]
print(total)
print(total.dtype)
print(total.index.equals(df.index))Adding an int64 column to a float64 column gives a float64 result. The result keeps the frame's index, so the last line prints True.
Why the model matters
Because each column has one dtype, the dtype decides three things:
- How much memory the column uses.
- Which operations are fast and which are slow.
- How missing values and mixed content are represented.
Choosing dtypes well is the main way to control the memory use of a large frame. The rest of this tutorial builds on that idea. It looks at the arrays behind each dtype, how pandas groups columns internally, and how to measure and reduce memory.
Common errors
- Assuming a column's dtype from what you typed. A column built from
[1, 2, 3]isint64, but[1, 2, 3.0]isfloat64. Always checkdf.dtypesafter building or loading data. - Treating
objectas "string". A column withobjectdtype can hold strings, but it can also hold any mix of Python objects. Check the dtype and the contents together. - Expecting one dtype per frame.
df.dtypesis a per-column report. A frame has no single dtype of its own.
Exercise
In the frame pd.DataFrame({"x": [1, 2], "y": ["a", "b"]}), what two things do the columns x and y share, and what can differ between them?
Show answer
They share the same row index (0 and 1). Their dtypes can differ: x is int64 and y is object in pandas 2.x.
Exercise
You compute df["a"] + df["b"] where a is int64 and b is float64. What dtype does the result have, and which index does it carry?
Show answer
The result is float64, and it carries the same index as the frame.