Skip to main content

Introduction to Pandas

Pandas is the most popular Python library for data manipulation and analysis. It provides easy-to-use data structures and data analysis tools for handling structured (tabular) data, similar to working with an Excel spreadsheet or SQL table.

Why use Pandas?

  • Easy handling of missing data.
  • Flexible tools for reshaping, pivoting, merging, and joining datasets.
  • Fast data loading from files (CSV, Excel, JSON, SQL databases).

Core Data Structures

Pandas has two primary data structures:
  1. Series: A 1D labeled array (like a single column in Excel).
  2. DataFrame: A 2D labeled data structure (like an entire Excel sheet/table).

Creating a DataFrame

import pandas as pd

# Creating a DataFrame from a dictionary
data = {
    "Name": ["Alice", "Bob", "Charlie"],
    "Age": [25, 30, 35],
    "City": ["New York", "London", "Paris"]
}

df = pd.DataFrame(data)
print(df)
Output:
      Name  Age      City
0    Alice   25  New York
1      Bob   30    London
2  Charlie   35     Paris

Basic Operations

Reading a CSV file

# Load dataset
df = pd.read_csv('data.csv')

# View the first 5 rows
print(df.head())

Filtering Data

# Get all people older than 28
older_than_28 = df[df['Age'] > 28]
print(older_than_28)

Next Steps

After manipulation, visualizing your data is key. Let’s explore how to create charts using Matplotlib.