Reference library

Big data & Spark

PySpark jobs, partitioning, batch processing, and large-dataset transform patterns.

6 matches
Big data & Spark easy

How to Explode an Array Column in Python

This code demonstrates a mock explode operation that converts an array column into multiple rows, similar to Spark's explode function.

explode arrays pyspark
Python
import json 

def explode_array_column(data, column):
    """Mock explode: split array column into multiple rows."""
    exploded = []
    for row in data:
        values = row.get(column, [])
        for value in values:
            new_row = dict(row)
            new_row[column] = value
            exploded.append(n…
13 0 Open
Big data & Spark easy

How to Filter and Project Spark DataFrames with PySpark SQL

Simulate a SQL SELECT with WHERE using PySpark DataFrame select and filter to project columns and apply conditions.

pyspark dataframe filter
Python
from pyspark.sql import SparkSession
from pyspark.sql.functions import col

spark = SparkSession.builder.appName("QueryFilterMock").master("local[2]").getOrCreate()

data = [
    ("Alice", 28, "Engineering"),
    ("Bob", 35, "Sales"),
    ("Carol", 32, "Engineering"),
    ("David", 25, "Marketing"),
    ("Eve", 29, "E…
14 0 Open
Big data & Spark medium

How to Implement row_number Window Function in Python

This code implements a SQL-style ROW_NUMBER() window function in pure Python, partitioning rows by a set of columns and ranking them within each partition by an ordered set of columns.

window-functions data-processing row-number
Python
from collections import defaultdict
import itertools


def row_number(rows, partition_by, order_by):
    partitions = defaultdict(list)
    for index, row in enumerate(rows):
        key = tuple(row[col] for col in partition_by)
        partitions[key].append((index, row))

    result = []
    for key in partitions:
 …
17 0 Open
Big data & Spark easy

How to Mock DataFrame Schema Columns in Python

Create an empty pandas DataFrame with only the specified column names to mock a schema before any data is loaded.

pandas dataframe schema
Python
import pandas as pd

def mock_schema(columns):
    return pd.DataFrame(columns=columns)

if __name__ == "__main__":
    cols = ["name", "age", "city"]
    df = mock_schema(cols)
    print(df)
    print(f"Columns: {list(df.columns)}, Shape: {df.shape}")
14 0 Open
Big data & Spark easy

How to select specific columns in Python with SQLite

A reusable function that connects to a SQLite database and returns only the requested columns from a given table.

sqlite sql database
Python
import sqlite3

def select_pruned_columns(db_path, table, columns):
    with sqlite3.connect(db_path) as conn:
        cursor = conn.cursor()
        col_list = ", ".join(columns)
        query = f"SELECT {col_list} FROM {table}"
        return cursor.execute(query).fetchall()

if __name__ == "__main__":
    conn = sq…
15 0 Open
Big data & Spark easy

Modeling a Hive Metastore Table Schema in Python

A dataclass that mimics a Hive metastore table schema—columns, partition keys, storage format, and location—with helper methods for description and mutation.

hive dataclass metastore
Python
from dataclasses import dataclass, field
from typing import Dict, List, Optional


@dataclass
class HiveTable:
    """Simple mock of a Hive metastore table schema."""
    name: str
    database: str = "default"
    columns: List[Dict[str, str]] = field(default_factory=list)
    partition_keys: List[Dict[str, str]] = f…
14 0 Open

Browse by section

Each section groups closely related Python snippets.

Big data & Spark — Python code examples

What you will find here

This page collects big data & spark snippets — short, copy-ready Python you can paste into our free online IDE and run without installing anything. Each sample includes a plain-English explanation and the full source code.

Samples vs tutorials and challenges

Samples are quick reference — one concept per page. For step-by-step teaching, use our Python tutorials. To test yourself, try quizzes or coding challenges. Clean up style with the Python formatter.