Featured Article

Simple Python Spark Example: Quick Start Guide

Kenneth Jul 13, 2026

Let's explore the power of Apache Spark, a powerful data processing engine, with a simple example using Python's PySpark API. If you're new to Spark, fear not! This hands-on guide will lead you through the basics with a step-by-step, real-world example.

python aesthetic
python aesthetic

To start, we'll need to set up our Python environment. Make sure you have Python 3.6 or later, and have installed `pyspark` using pip:

Python: Simple Rest API Example and String Formatting
Python: Simple Rest API Example and String Formatting

Setting up the Environment

First, you need to ensure you have both Spark and PySpark installed. Download and extract Spark from the official website, then install PySpark using pip:

Variables in Python Explained (Super Simple 🔥)
Variables in Python Explained (Super Simple 🔥)

```bash pip install pyspark ```

Importing PySpark and Initializing a SparkSession

Reverse a List in Python (Easy Method for Beginners)
Reverse a List in Python (Easy Method for Beginners)

The first step is to import the necessary modules and initialize a SparkSession. This is the entry point to any functionality in Spark:

```python from pyspark.sql import SparkSession spark = SparkSession.builder \ .appName('Simple Python Spark Example') \ .getOrCreate() ```

Creating a DataFrame from a List

All Important Python Functions for Beginners (Complete Cheat Sheet)
All Important Python Functions for Beginners (Complete Cheat Sheet)

Spark DataFrames are similar to tables in a relational database. Let's create a simple DataFrame from a list of tuples:

```python data = [("John", 30), ("Anna", 25), ("Peter", 35)] columns = ["Name", "Age"] df = spark.createDataFrame(data, columns) df.show() ```

Reading and Writing Data

Python Notes for Beginners (Easy + Quick Guide)
Python Notes for Beginners (Easy + Quick Guide)

Now let's explore how to read and write data using PySpark.

Reading CSV Files

a computer screen with an animated scatter plot in python
a computer screen with an animated scatter plot in python
an image of a screen shot of a web page with the text animated scatter plot using python
an image of a screen shot of a web page with the text animated scatter plot using python
the top 50 python project ideas
the top 50 python project ideas
Python Basics Cheat Sheet for Beginners | Learn Coding Step by Step
Python Basics Cheat Sheet for Beginners | Learn Coding Step by Step
three different types of python programming
three different types of python programming
Factorial of a Number in Python (Beginner-Friendly with Output)
Factorial of a Number in Python (Beginner-Friendly with Output)
15 Essential Python String Functions Every Beginner Must Know (Python Cheat Sheet)
15 Essential Python String Functions Every Beginner Must Know (Python Cheat Sheet)
Python Tip: Use zip() Like a Pro 🐍
Python Tip: Use zip() Like a Pro 🐍

Spark can read data from a variety of sources. Let's read a CSV file using PySpark:

```python df = spark.read.csv('path/to/your/file.csv', header=True, inferSchema=True) df.show() ```

Writing Data to CSV

Writing data is just as easy. Here's how you can write a DataFrame to a CSV file:

```python df.write.csv('path/to/output.csv') ```

Data Processing with PySpark

Transforming data is where Spark truly shines. Let's perform some basic data processing on our DataFrame.

Aggregating Data

Spark provides a variety of high-level transformations and actions for data processing. Let's calculate the average age from our DataFrame:

```python avg_age = df.agg({'Age': 'avg'}).collect()[0][0] print(f'The average age is: {avg_age}') ```

Filtering Data

You can also filter data based on certain conditions. Let's find all people who are older than 30:

```python older_than_30 = df.filter(df['Age'] > 30) older_than_30.show() ```

So there you have it! You've now created a DataFrame, read and written data, and performed basic data processing. There's much more to explore in the world of PySpark, so happy coding!