The DataFrame is the spreadsheet of Python — labeled rows and columns you can slice, filter, and transform. Meet the Series, the DataFrame, and the index.
Why: a DataFrame is a table where each column is a Series (a labeled 1-D array), and it is the object you will spend most of your data work inside. When: use it for any tabular data — rows are records, columns are features. Where: every column has its own dtype, unlike a NumPy array.
pip install pandasWhy: before doing anything you look at the data — its shape, column types, and a few rows — to know what you are working with. When: run head(), info(), and describe() first, every single time. Where: the index is the row labels (a default RangeIndex unless you set one).
import pandas as pd
df = pd.DataFrame({
"name": ["Ada", "Linus", "Grace", "Alan"],
"age": [36, 29, 45, 41],
"role": ["eng", "eng", "sci", "sci"],
})
print(df.head()) # first rows
print(df.shape) # (4, 3)
print(df.info()) # columns, non-null counts, dtypes
print(df.describe()) # summary stats for numeric columnsWhy: selecting a column gives you a Series you can run vectorized operations on, exactly like NumPy but with labels. When: add derived columns by assigning the result of an expression. Where: bracket access with a list returns a DataFrame (multiple columns), a single name returns a Series.
ages = df["age"] # a Series
print(ages.mean(), ages.max())
# Add a new column from a vectorized expression:
df["age_in_5"] = df["age"] + 5
# Select several columns (note the double brackets -> DataFrame):
print(df[["name", "role"]])
# Value counts — how many of each category:
print(df["role"].value_counts())