← Back to list

From Pandas to PySpark: The Beginner’s Guide to Big Data in Python

Your Pandas code works great — until the data gets too big. This beginner’s PySpark guide covers everything you need: SparkSession…

Isha Shaw in Data Science Collective · 2026-06-10 06:06 · 5 claps · 7.8 min read
#data-science #data-engineering #software-engineering #software-development #artificial-intelligence
Open on Medium ↗
Wiki topics: ML · Machine Learning AI · AI · General 🔧 · Data Engineering 🔬 · Science · General

From Pandas to PySpark: The Beginner’s Guide to Big Data in Python

Your Pandas code works great — until the data gets too big. This beginner’s PySpark guide covers everything you need: SparkSession, DataFrames, lazy evaluation, transformations, and your first real PySpark program.

You’ve been writing Python and Pandas for a while now. Filters, groupbys, merges, the works. It runs fast, it makes sense, and honestly, it’s been fine. Then one day you load a dataset that’s bigger than usual. Maybe it’s a year’s worth of transaction logs instead of a month. Maybe someone handed you a 20 GB CSV from an S3 bucket. You run your usual code and watch your machine think. And think. And think. Then it crashes with a MemoryError and you sit there wondering what you’re supposed to do now. This is the moment almost every Python data person eventually hits. And it’s the exact moment PySpark was built for.

AI generated image

AI generated image

This guide covers what PySpark actually is, why it solves the problem Pandas can’t, and how to write your first real PySpark program from scratch, even if you’ve never heard the words “distributed computing” before today.

What Is PySpark?

PySpark is the Python interface for Apache Spark, an open-source engine that processes massive datasets by splitting the work across multiple machines simultaneously rather than running everything on one.

The mental model is straightforward: Python is the language you already know and write in. Apache Spark is the distributed computing engine doing the heavy lifting underneath. PySpark is what lets you talk to Spark using Python, without ever touching Scala or Java.

While Pandas loads your entire dataset into your laptop’s RAM and processes it there, PySpark breaks that same dataset into chunks and distributes those chunks across a cluster of machines, all processing in parallel. A job that takes two hours on a single machine might take four minutes when ten machines are doing it together.

Why Should You Learn PySpark If You Already Know Pandas?

For small datasets, say anything under a few gigabytes that fits comfortably in your machine’s memory, Pandas is faster to write, simpler to run, and perfectly sufficient. Nobody should reach for PySpark to analyse a 500 MB file.

But data engineering and data science work at serious companies doesn’t live at 500 MB. It lives at hundreds of gigabytes, terabytes, sometimes petabytes. At that scale, Pandas doesn’t just slow down. It stops working.

The simple rule that experienced engineers use: start with Pandas, switch to PySpark when the data outgrows the machine. Knowing when to make that switch, and how to execute it, is a genuinely valuable skill in 2025.

How PySpark Works: The Concepts That Actually Matter

SparkSession: Your Starting Point

Every PySpark program begins with a SparkSession. Think of it as opening a connection to the Spark engine. Nothing works without it, and you only need to create it once at the top of your script.

from pyspark.sql import SparkSession

spark = SparkSession.builder \
    .appName("My First PySpark App") \
    .getOrCreate()

The appName is just a label that shows up in the Spark UI when you're monitoring jobs. getOrCreate() means Spark will reuse an existing session if one already exists, which prevents accidentally spinning up multiple engines in the same environment.

DataFrames: The Structure You’ll Work With Every Day

A PySpark DataFrame is conceptually identical to a Pandas DataFrame or a SQL table: rows and columns of structured data. If you know how to think about tabular data, you already understand the structure.

# Load a CSV file into a DataFrame
df = spark.read.csv("users.csv", header=True, inferSchema=True)
# See the first five rows
df.show(5)
# Check the schema (column names and data types)
df.printSchema()

The critical difference between a Pandas DataFrame and a PySpark DataFrame isn’t the structure, it’s where the data lives. A Pandas DataFrame exists entirely on your machine. A PySpark DataFrame is distributed and its data is partitioned across multiple machines in the cluster, and all those machines can process their partitions simultaneously.

That distribution is invisible when you’re writing code. You use the same API. The engine handles where data lives.

Technical note worth knowing: PySpark DataFrames are built on top of an older API called RDDs (Resilient Distributed Datasets). RDDs are the lower level, more flexible foundation. DataFrames are the modern high level API that sits on top of RDDs and adds SQL style optimisation. As a beginner you should use DataFrames exclusively. RDDs exist and matter, but you don’t need them until you’re doing something DataFrames genuinely can’t handle.

Transformations vs. Actions

This is the single most important thing to understand about PySpark, and it’s also the most counterintuitive. In Pandas, when you write df[df["age"] > 25], it runs immediately. The result is in memory the moment that line executes.

PySpark doesn’t work this way. Transformations describe what you want to do with the data. They don’t execute immediately. They build up a plan.

# None of these lines actually run yet
df_filtered = df.filter(df["age"] > 25)
df_selected = df_filtered.select("name", "country", "age")
df_grouped = df_selected.groupBy("country").count()

At this point, Spark has recorded your intentions but hasn’t touched a single row of data. Actions are what trigger execution. The moment you call an action, Spark takes everything it recorded, optimises the full plan, and runs it.

# THIS is when all three steps above actually execute
df_grouped.show()

This design is called lazy evaluation, and it’s one of the main reasons PySpark is fast.

Lazy Evaluation and the DAG: Why PySpark Is Fast

When you call an action, Spark doesn’t just run your steps in order. It first builds a DAG (Directed Acyclic Graph), which is essentially a complete blueprint of your job. It then runs that blueprint through an optimiser called Catalyst, which looks for redundancies, reorders operations where possible, and eliminates work that doesn’t affect the final result.

A concrete example: if you filter a billion row dataset down to 10,000 rows and then run a complex aggregation, Spark’s optimiser knows to apply the filter as early as possible so the aggregation only touches 10,000 rows instead of a billion. You didn’t have to think about that. The optimiser handled it. You can see what plan Spark generated for any DataFrame operation by calling:

df_grouped.explain(verbose=True)

Reading the output of explain() looks intimidating at first. But even glancing at it teaches you a lot about how your code is actually being executed.

PySpark Transformations You’ll Use in Almost Every Job

These are the operations that show up in real production pipelines:

# Select specific columns (everything else is ignored)
df.select("name", "age", "country")

# Filter rows on a condition
df.filter(df["age"] > 25)

# Multiple conditions (use & for AND, | for OR, with parentheses)
df.filter((df["age"] > 25) & (df["country"] == "India"))

# Group and aggregate
df.groupBy("country").count()
df.groupBy("country").agg({"age": "avg", "salary": "sum"})

# Add a calculated column
df.withColumn("age_next_year", df["age"] + 1)

# Rename a column
df.withColumnRenamed("old_name", "new_name")

# Remove duplicates
df.dropDuplicates()
df.dropDuplicates(["email"])  # deduplicate on a specific column

# Sort results
df.orderBy("age", ascending=False)

# Drop a column you no longer need
df.drop("unnecessary_column")

Nothing here should feel unfamiliar if you’ve used Pandas. The syntax is different but the logic maps directly.

Joins in PySpark

Joins work similarly to SQL. The most common types you’ll encounter:

# Inner join (only matching rows from both DataFrames)
orders.join(customers, on="customer_id", how="inner")

# Left join (all rows from left, matching rows from right)
orders.join(customers, on="customer_id", how="left")

# Broadcast join (use this when one DataFrame is small)
from pyspark.sql.functions import broadcast
orders.join(broadcast(lookup_table), on="product_id", how="inner")

Broadcast joins matter in production: When one of your DataFrames is small (a lookup table, a reference list, anything under a few hundred MB), wrapping it in broadcast() tells Spark to send a copy of that small table to every machine rather than shuffling data across the network. This can turn a slow, network heavy join into a fast local operation. It's one of the first optimizations experienced PySpark engineers reach for.

Reading and Writing Data

Reading Different Formats

# CSV
df = spark.read.csv("data/users.csv", header=True, inferSchema=True)

# Parquet (the format you'll use most in production)
df = spark.read.parquet("data/users.parquet")

# JSON
df = spark.read.json("data/events.json")

# Reading directly from cloud storage (same API, different path)
df = spark.read.parquet("gs://your-bucket/data/users.parquet")   # GCS
df = spark.read.parquet("s3a://your-bucket/data/users.parquet")  # S3

Writing Your Results

# Write as Parquet (recommended for almost everything)
df.write.mode("overwrite").parquet("output/results.parquet")
# Write as CSV (when the consumer needs it)
df.write.mode("overwrite").csv("output/results/", header=True)
# Write with partitioning (speeds up downstream queries dramatically)
df.write.mode("overwrite").partitionBy("country").parquet("output/results/")

Always prefer Parquet over CSV for big data. Parquet is a columnar format, which means analytical queries that only need a few columns can skip reading the rest entirely. It also compresses far better than CSV. A 10 GB CSV file is often 1 to 2 GB as Parquet. In production pipelines, Parquet is the default for almost everything.

Your First Real PySpark Program

Here’s a complete, production style script that loads a dataset, filters it, aggregates it, and saves the result. This is the pattern real data pipelines follow:

from pyspark.sql import SparkSession
from pyspark.sql.functions import col, count

# Step 1: Start the session
spark = SparkSession.builder \
    .appName("User Country Analysis") \
    .getOrCreate()

# Step 2: Load data
df = spark.read.csv("users.csv", header=True, inferSchema=True)

# Step 3: Inspect what you loaded
print(f"Total rows: {df.count()}")
df.printSchema()

# Step 4: Transformations (lazy, nothing runs yet)
df_adults = df.filter(col("age") > 25)
df_clean = df_adults.dropDuplicates(["user_id"])
result = df_clean.groupBy("country").count().orderBy("count", ascending=False)

# Step 5: Action (now everything above runs)
result.show(20)

# Step 6: Save the output
result.write.mode("overwrite").parquet("output/user_counts_by_country.parquet")

# Step 7: Always stop the session when done
spark.stop()

Every real PySpark job you write will follow this same skeleton. Session, load, transform, action, save, stop.

Running PySpark on Your Laptop

You don’t need a cloud cluster or a corporate Hadoop environment to learn PySpark. It runs in local mode on any machine, using your laptop’s cores to simulate a cluster.

pip install pyspark

That’s it. Once installed, the code examples above all run locally. PySpark in local mode uses all available CPU cores on your machine, which is more than enough to learn with and test against real datasets.

When you’re ready to move to production, the same code runs on Google Dataproc, Amazon EMR, Azure HDInsight, or Databricks without modification. The API is identical. The cluster handles the scale.

What to Learn After This

Once the basics are solid, these are the topics that separate beginner PySpark users from engineers who can build production pipelines:

Spark SQL lets you run SQL queries directly on DataFrames using spark.sql("SELECT ..."), which many data engineers prefer for complex aggregations.

Window functions are essential for running totals, rankings, moving averages, and any calculation that needs to look at neighboring rows. They’re one of the most powerful features in PySpark and show up constantly in real pipelines.

Partitioning and caching are how you optimize slow jobs. Understanding how data is physically distributed across the cluster, and how to control that distribution, is what separates fast PySpark code from slow PySpark code.

Structured Streaming extends the same DataFrame API to handle real time data streams from Kafka or other sources. Once you understand batch PySpark, streaming is a surprisingly small conceptual jump.

Airflow integration is how production PySpark jobs get scheduled and monitored. Most enterprise data pipelines are PySpark jobs orchestrated by Airflow and knowing how the two fit together a near requirement for data engineering roles is.

Final Thoughts

PySpark has a learning curve, but it’s not as steep as it first appears, especially for Python developers who already think in terms of DataFrames and tabular data. The concepts that feel foreign (lazy evaluation, distributed execution, the DAG) become intuitive quickly once you’ve run a few real jobs and watched the execution plan work.

The best thing you can do right now is install PySpark locally with pip install pyspark, open a notebook, and run the examples in this guide against a real dataset. Reading about distributed computing is useful. Watching a billion row filter execute in seconds on your own machine is what makes it real.


메타데이터
post_id
9b2dce704ce0
slug
from-pandas-to-pyspark-the-beginners-guide-to-big-data-in-python-9b2dce704ce0
url
https://medium.com/data-science-collective/from-pandas-to-pyspark-the-beginners-guide-to-big-data-in-python-9b2dce704ce0
canonical_url
https://medium.com/data-science-collective/from-pandas-to-pyspark-the-beginners-guide-to-big-data-in-python-9b2dce704ce0
author_url
https://medium.com/@isha372002
status
ok
fetched_at
2026-06-15 20:49:13