Python

Data Analysis with Python & Pandas

Introduction

Data is the backbone of modern decision-making. In my work delivering MIS and EMIS platforms for governments and UN agencies, Python and Pandas have been indispensable for transforming raw data exports into actionable insights for senior stakeholders. This guide walks through the essential workflow.

Step 1 โ€” Set Up Your Environment

Start by creating an isolated Python environment and installing the core libraries:

python -m venv venv
source venv/bin/activate   # Windows: venv\Scripts\activate
pip install pandas numpy matplotlib openpyxl

Step 2 โ€” Load Your Dataset

Pandas makes it trivial to load data from CSV, Excel, or databases. Always inspect your data immediately after loading:

import pandas as pd

# Load CSV
df = pd.read_csv('beneficiaries.csv')

# Quick inspection
print(df.shape)       # (rows, columns)
print(df.dtypes)      # column types
print(df.head())      # first 5 rows
print(df.isnull().sum())  # missing values per column

๐Ÿ’ก Tip: Always check df.isnull().sum() immediately. In government datasets, missing values in key identifier columns can silently corrupt your analysis.

Step 3 โ€” Clean & Transform

Real-world datasets are rarely clean. Here are the transformations I apply on almost every project:

# Drop duplicates
df = df.drop_duplicates(subset=['beneficiary_id'])

# Fill or drop nulls
df['district'] = df['district'].fillna('Unknown')
df = df.dropna(subset=['date_of_birth'])

# Parse dates
df['enrollment_date'] = pd.to_datetime(df['enrollment_date'], errors='coerce')

# Normalize text columns
df['district'] = df['district'].str.strip().str.title()

Step 4 โ€” Aggregate & Analyse

With clean data, generate summary statistics by group โ€” the bread and butter of management reporting:

# Beneficiaries by district
summary = df.groupby('district').agg(
    total=('beneficiary_id', 'count'),
    female=('gender', lambda x: (x == 'Female').sum()),
    avg_age=('age', 'mean')
).reset_index()

print(summary.sort_values('total', ascending=False).head(10))

Step 5 โ€” Export Results

Export your cleaned, aggregated data for dashboards or donor reports:

# Excel report with multiple sheets
with pd.ExcelWriter('analysis_report.xlsx', engine='openpyxl') as writer:
    df.to_excel(writer, sheet_name='Clean Data', index=False)
    summary.to_excel(writer, sheet_name='District Summary', index=False)

print("Report exported successfully.")

Conclusion

This five-step workflow โ€” environment, load, clean, analyse, export โ€” applies whether you're processing 500 rows or 5 million. Python and Pandas give you the power to automate what would otherwise take days of manual spreadsheet work, delivering insights that drive real program decisions.

← Back to Articles Next: Django REST APIs →