Skip to main content
Real-world data is almost never ready to use straight out of the box. It arrives as messy CSV files, JSON exports, or database dumps — incomplete, inconsistently formatted, and riddled with missing values. Pandas is the Python library that bridges the gap between raw data and a clean, analysis-ready dataset. It gives you two powerful data structures, a rich API for filtering and transformation, and seamless integration with NumPy, Matplotlib, and Scikit-learn. Before you write a single line of machine learning code, you’ll spend a significant amount of time here.

Installation and Import

Pandas Data Structures

Series (1D)

A Series is a one-dimensional labeled array. Unlike a Python list, it comes with built-in statistical methods.

DataFrame (2D)

A DataFrame is a two-dimensional table with labeled rows and columns — think of it as a spreadsheet or SQL table in Python. Unlike a NumPy 2D array, a DataFrame can hold different data types in different columns.

Creating DataFrames from Files

For large datasets, prefer Parquet or Feather formats over CSV. A 1 GB CSV file can compress to ~100 MB in Parquet — roughly 10× smaller — while also being faster to read and write.

Exploring a Dataset

Accessing Data

loc[] — Label-Based Indexing

loc uses row labels and column names. Slicing with loc is inclusive of both endpoints.

iloc[] — Integer Position-Based Indexing

iloc uses integer positions. Slicing is exclusive of the upper bound (Python-style).

at[] and iat[] — Fast Scalar Access

Column Access

Prefer bracket notation df['column'] over dot notation df.column. Dot notation fails silently when a column name contains spaces or matches a built-in DataFrame attribute.

Filtering Data

Always wrap each condition in its own parentheses when combining with & or |. Without the parentheses, Python’s operator precedence will produce unexpected results or raise a TypeError.

Updating and Transforming Data

Updating with loc

Transforming with apply()

Faster Conditionals with np.where()

For simple conditions, np.where() is significantly faster than apply() because it is fully vectorized.
When to use each:

Column Operations

Combining DataFrames

Merging (SQL-style Joins)

Concatenating (Stacking)

Handling Missing Values

Data Aggregation

Working with Strings

Working with Dates