Search Knowledge

© 2026 LIBREUNI PROJECT

Python for Scientific Computing / Pandas Deep Dive

Pandas: High-Performance Data Structures

Why Pandas?

While NumPy provides the computational horsepower for numerical arrays, it lacks the high-level features needed for real-world data analysis, such as:

  • Handling missing data (NaN).
  • Working with labeled axes (instead of just integer indices).
  • Merging and joining datasets (SQL-like operations).
  • Time-series functionality.

Pandas builds on top of NumPy to provide these features. It is the primary tool for data “munging” or “wrangling”—the process of cleaning and transforming raw data into a format suitable for analysis.

Core Data Structures: Series and DataFrame

There are two primary data structures in Pandas:

  1. Series: A 1D array-like object with an associated index.
  2. DataFrame: A 2D table-like structure with labeled rows and columns.

The Series Object

Think of a Series as a cross between a NumPy array and a Python dictionary.

python
1 
2import pandas as pd
3import numpy as np
4 
5# Creating a Series from a list
6data = pd.Series([0.25, 0.5, 0.75, 1.0], index=['a', 'b', 'c', 'd'])
7print("Series:\n", data)
8 
9# Accessing by label
10print("\nValue at index 'b':", data['b'])
11 
12# Series behaves like a numpy array
13print("\nMean of Series:", data.mean())
14 

The DataFrame Object

The DataFrame is the most important structure in Pandas. It represents a table of data, similar to a spreadsheet or a SQL table. Each column in a DataFrame is itself a Series.

python
1 
2import pandas as pd
3 
4data = {
5 'Name': ['Alice', 'Bob', 'Charlie', 'David'],
6 'Age': [25, 30, 35, 40],
7 'City': ['New York', 'London', 'Paris', 'Tokyo']
8}
9 
10df = pd.DataFrame(data)
11print("DataFrame:\n", df)
12 
13# Check basic info
14print("\nColumns:", df.columns)
15print("Index:", df.index)
16 

Data Selection and Indexing

Selection in Pandas can be confusing because there are multiple ways to do it.

1. loc: Label-based selection

loc is used to select data using the labels of rows and columns.

2. iloc: Integer-based selection

iloc is used to select data using the 0-based integer position.

python
1 
2import pandas as pd
3 
4df = pd.DataFrame({
5 'A': [1, 2, 3],
6 'B': [4, 5, 6]
7}, index=['row1', 'row2', 'row3'])
8 
9print("Selection with loc (labels):\n", df.loc['row2', 'A'])
10print("\nSelection with iloc (positions):\n", df.iloc[1, 0])
11 

Importing Data

In practice, you rarely create DataFrames by hand. You load them from files (CSV, Excel, JSON, SQL).

# Reading a CSV file
df = pd.read_csv('data.csv')

# Writing to an Excel file
df.to_excel('output.xlsx')

Handling Missing Data

Real-world data is messy. Pandas uses NaN (Not a Number) to represent missing values and provides robust tools to handle them.

python
1 
2import pandas as pd
3import numpy as np
4 
5df = pd.DataFrame({
6 'A': [1, 2, np.nan, 4],
7 'B': [5, np.nan, np.nan, 8],
8 'C': [1, 2, 3, 4]
9})
10 
11print("Original DF with NaNs:\n", df)
12 
13# Fill NaNs with a value
14print("\nFilled NaNs:\n", df.fillna(value=0))
15 
16# Drop rows with any NaN
17print("\nDropped NaNs:\n", df.dropna())
18 

GroupBy and Aggregation

Similar to GROUP BY in SQL, Pandas allows you to split data into groups and apply functions to each group separately.

python
1 
2import pandas as pd
3 
4df = pd.DataFrame({
5 'Company': ['GOOG', 'GOOG', 'MSFT', 'MSFT', 'FB', 'FB'],
6 'Person': ['Sam', 'Charlie', 'Amy', 'Vanessa', 'Carl', 'Sarah'],
7 'Sales': [200, 120, 340, 124, 243, 350]
8})
9 
10by_comp = df.groupby("Company")
11print("Mean sales per company:\n", by_comp['Sales'].mean())
12 

In the following modules, we will explore advanced joining techniques and time-series analysis.