Intro to Pandas
pandas is Python's library for working with tabular data — anything you'd naturally think of as rows and columns, like a spreadsheet or a database table. It's built on top of NumPy, so its columns are stored as fast, typed arrays under the hood, but it adds the things NumPy doesn't have: labelled rows and columns, mixed column types in a single table, missing-value handling, and a huge set of convenience methods for reading, cleaning, filtering, and summarizing data.
Installing pandas
pip install pandas
DataFrame and Series
pandas has two core objects. A Series is a single labelled column of data — essentially a NumPy array with an index attached. A DataFrame is a full table: a collection of Series sharing the same row index, one per column.
import pandas as pd
# A Series — one labelled column
ages = pd.Series([25, 32, 18], name="age")
# A DataFrame — built from a dict of columns
df = pd.DataFrame({
"name": ["Ada", "Grace", "Alan"],
"age": [36, 30, 41],
"city": ["London", "New York", "London"],
})
print(df)name age city 0 Ada 36 London 1 Grace 30 New York 2 Alan 41 London
Reading data from a file
In practice, you rarely build a DataFrame by hand — you load it from a file, most commonly a CSV:
import pandas as pd
df = pd.read_csv("employees.csv")pandas also has readers for many other formats out of the box, including read_excel(), read_json(), and read_sql().
Exploring a DataFrame
Before doing anything with a new dataset, it's standard practice to get a quick feel for its shape and contents:
df.head() # first 5 rows — quick sanity check df.info() # column names, dtypes, non-null counts df.describe() # count, mean, std, min/max, quartiles for numeric columns
.head(n)— the firstnrows (default 5). There is also.tail(n)for the last rows..info()— column dtypes and how many non-null values each column has, useful for spotting missing data quickly..describe()— summary statistics (mean, standard deviation, min, max, percentiles) for every numeric column..shape— a(rows, columns)tuple, just like a NumPy array.
Selecting columns and rows
pandas gives you three main ways to select data, and it's worth knowing when to use each:
df["name"] # a single column, returned as a Series df[["name", "age"]] # multiple columns, returned as a DataFrame df.loc[0] # row selected by label/index value df.loc[0, "age"] # a specific cell, by row label + column name df.iloc[0] # row selected by integer position df.iloc[0:2, 0:2] # first 2 rows, first 2 columns, by position
[]— quick column access (or a boolean mask, see below)..loc— select by label (row index value, column name). Use this when your rows have meaningful labels, not just 0, 1, 2.....iloc— select by integer position, the same way you would index a plain Python list.
Filtering with boolean conditions
The most common pandas idiom is filtering rows with a boolean condition inside [] — this is often called boolean masking:
# Rows where age is greater than 30 df[df["age"] > 30] # Combine conditions with & and | (note the parentheses!) df[(df["age"] > 30) & (df["city"] == "London")]