Series and DataFrame
The two data structures, what the index really is, and how to inspect a new dataset before touching it.
Series: a labelled column
A Series is a one-dimensional array with an index — labels attached to positions. Almost every confusing pandas behaviour comes from the index quietly participating in alignment.
import pandas as pd
s = pd.Series([10, 20, 30], index=["a", "b", "c"])
s["b"] # 20 - by label
s.iloc[1] # 20 - by position
s.to_numpy() # array([10, 20, 30])💡
.loc means labels, .iloc means positions. Deciding this once removes most of the confusion in pandas.DataFrame: a table
df = pd.DataFrame({
"name": ["Ada", "Grace", "Alan"],
"score": [88, 95, 71],
"team": ["A", "B", "A"]
})
df.shape # (3, 3)
df.columns # Index(['name', 'score', 'team'], dtype='object')
df.dtypes
df.head(2)
df.describe() # numeric summary
df.info() # types + non-null counts: the first thing to run- Each column is a Series sharing the row index.
- Row labels may be integers, dates or strings; they are not the same as row numbers.
- A default
RangeIndexlooks like positions but is still an index — filtering keeps the original labels, which is a classic source of surprise.
Automatic alignment
When you combine two Series, pandas matches them by label, not by position. Labels that do not match produce NaN instead of an error.
a = pd.Series([1, 2, 3], index=["x", "y", "z"])
b = pd.Series([10, 20, 30], index=["z", "y", "x"])
a + b
# x 31
# y 22
# z 13
# both were matched by label - order was irrelevant⚠️
After filtering, the index has gaps (
0, 2, 5 …). Mixing position and label logic on that index is how people silently attach the wrong values. When you need positional logic, use .iloc or call .reset_index(drop=True) deliberately.FAQ
How do I rename a column?
df.rename(columns={"old": "new"}). Assign the result (or pass inplace=True) — the method returns a new frame by default.Why is my column called object?
object is pandas' dtype for text (or mixed types). It is slow and memory-hungry; for genuinely text columns consider string dtype.Related
Reading and writing data Selecting, filtering and cleaning
Last refreshed 2026-09-18.