← Back to Chapters

Pandas Tutorial

? Pandas Tutorial

? Quick Overview

Pandas is a powerful Python library for data manipulation and analysis. It provides two main data structures: Series for one-dimensional data and DataFrame for tabular, two-dimensional data.

With Pandas you can easily load, clean, transform, and analyze structured data from sources like CSV, Excel, SQL databases, and more.

? Key Concepts

  • Series: A one-dimensional labeled array (similar to a column in a spreadsheet).
  • DataFrame: A two-dimensional labeled data structure with rows and columns.
  • Index: Labels that identify rows (and sometimes columns).
  • loc: Label-based selection of rows and columns.
  • iloc: Integer position-based selection of rows and columns.

? Syntax & Theory

To use Pandas, you first install it with pip and then import it in your Python script, usually with the alias pd.

A Series can be created from a Python list, NumPy array, or dictionary. A DataFrame is often created from a dictionary of lists or from external data files (like CSV).

? Series Basics

A Series stores a single column of data with an index. If you don't specify the index, Pandas creates a default integer index starting from 0.

? DataFrame Basics

A DataFrame is like a table: each column is a Series, and each row has an index. You can access columns by name and rows by label or integer position.

? Code Examples

⬇️ Installing Pandas

? View Code Example
# Install pandas library
pip install pandas

? Creating a Series

? View Code Example
import pandas as pd # Import the pandas library

data = [10, 20, 30, 40] # Define a list of numbers
series = pd.Series(data) # Create a Series from the list
print(series) # Display the Series

? Creating a DataFrame

? View Code Example
import pandas as pd # Import the pandas library

data = { # Dictionary to build the DataFrame
    "Name": ["Alice", "Bob", "Charlie"],
    "Age": [25, 30, 35],
    "City": ["New York", "Los Angeles", "Chicago"]
}

df = pd.DataFrame(data) # Create a DataFrame from the dictionary
print(df) # Display the DataFrame

? Accessing Data with loc and iloc

? View Code Example
# Access a single column
print(df["Name"])

# Access multiple columns
print(df[["Name", "Age"]])

# Access rows by label (index)
print(df.loc[0])

# Access rows by integer position
print(df.iloc[0])

? Live Output & Explanation

? Example: Series Output

If you run the Series example, the output will look like this:

0    10
1    20
2    30
3    40
dtype: int64

The left side (0, 1, 2, 3) is the index, and the right side is the value. dtype shows the data type of the values.

? Example: DataFrame Output

For the DataFrame example, the output will look like a table:

      Name  Age         City
0    Alice   25     New York
1      Bob   30  Los Angeles
2  Charlie   35      Chicago

Each row has an index (0, 1, 2) and each column (Name, Age, City) has its own label.

? Use Cases

  • Reading and analyzing CSV/Excel reports.
  • Cleaning messy real-world data (missing values, duplicates).
  • Filtering, grouping, and aggregating data for dashboards.
  • Preparing datasets for machine learning models.

? Tips & Best Practices

  • Use DataFrame for tabular data and Series for 1D data.
  • Use loc for label-based indexing and iloc for integer-based indexing.
  • Check data types with df.dtypes before performing operations.
  • Use copy() when needed to avoid unintentionally modifying original data.
  • Pandas works very well with NumPy arrays for efficient numerical computations.

? Try It Yourself

  • Create a DataFrame for your favorite books and their authors.
  • Add columns like rating, year, or genre and print the full table.
  • Access specific rows and columns using loc and iloc.
  • Filter the DataFrame to show only rows matching some condition (e.g., rating > 4).
  • Add a new column based on an existing one (for example, a boolean column like Is_New = Year >= 2020).