Featured Article

Master Azure Synapse PySpark with Real World Example Code

Kenneth Jul 13, 2026

Azure Synapse Pyspark offers a powerful combination of Apache Spark and Azure's robust cloud capabilities, enabling data professionals to run Pyspark code in the cloud seamlessly. Let's delve into an example of using Pyspark with Azure Synapse to understand its capabilities better.

Azure Synapse is taking Data Driven Enterprises to the Next Level
Azure Synapse is taking Data Driven Enterprises to the Next Level

A common scenario is analyzing big data. Here, we'll demonstrate how to read a CSV file, perform simple transformations, and save the results back to Azure Blob Storage using Pyspark with Azure Synapse.

screenshot of the web page for high - throughputing data logger in windows 10
screenshot of the web page for high - throughputing data logger in windows 10

Setting Up the Environment

First, ensure you have an Azure Synapse workspace set up with a SQL pool and an Apache Spark pool. For this example, we'll use the Spark pool.

Microsoft Unveils Secure MCU Platform with a Linux-Based OS - Circuit Cellar
Microsoft Unveils Secure MCU Platform with a Linux-Based OS - Circuit Cellar

Next, upload your data to an Azure Blob Storage container. We'll use this as our source data.

Reading Data from Azure Blob Storage

Explore Azure Machine Learning workspace resources and assets - Training
Explore Azure Machine Learning workspace resources and assets - Training

To read data, we use the Spark 'read' function and specify the file format and location. Here's how you can read a CSV file:

```python df = spark.read.csv("abfss://@.dfs.core.windows.net//.csv", inferSchema=True, header=True) ```

Performing Transformations

Using Azure-Functions-for-Python to create a Cognitive Search Filtering Custom Skill | Microsoft Community Hub
Using Azure-Functions-for-Python to create a Cognitive Search Filtering Custom Skill | Microsoft Community Hub

Now let's perform some simple transformations. Suppose our CSV file has 'Name', 'Age', and 'Salary' columns. We can filter data and aggregate as follows:

```python from pyspark.sql.functions import avg, count # Filter data for employees earning more than 50000 filtered_df = df.filter(df['Salary'] > 50000) # Calculate average salary and count of employees results = filtered_df.agg(avg('Salary').alias('avg_salary'), count('*').alias('count')) ```

Writing Data Back to Azure Blob Storage

5 Handy Built-In Windows Apps You Probably Didn't Know Exist - SlashGear
5 Handy Built-In Windows Apps You Probably Didn't Know Exist - SlashGear

After transforming your data, you might want to write it back to Azure Blob Storage. You can do this using Spark's 'write' function:

```python results.write.mode('overwrite').parquet("abfss://@.dfs.core.windows.net//results.parquet") ```

the dashboard shows data from different countries in blue and white, as well as several other graphs
the dashboard shows data from different countries in blue and white, as well as several other graphs
Fullstack Serverless Blazor App With Azure Functions
Fullstack Serverless Blazor App With Azure Functions
Scientists develop a memristor that can be conditioned just like a real synapse
Scientists develop a memristor that can be conditioned just like a real synapse
Azure Machine Learning
Azure Machine Learning
Azure Firewall Basic (Preview)
Azure Firewall Basic (Preview)
Master Async/Await in TypeScript With Proper Typing
Master Async/Await in TypeScript With Proper Typing
Explore Azure Cognitive Services
Explore Azure Cognitive Services
Purine & Pyrimidine Synthesis (de-novo) | EasyBiologyClass
Purine & Pyrimidine Synthesis (de-novo) | EasyBiologyClass
two diagrams showing the different types of syns and nematic nervous systems, one with
two diagrams showing the different types of syns and nematic nervous systems, one with

Viewing the Results

To view the results, you can read the parquet file back into a DataFrame or use Azure tools to visualize the data.

```python results = spark.read.parquet("abfss://@.dfs.core.windows.net//results.parquet") results.show() ```

This basic example demonstrates how to use Pyspark with Azure Synapse to read, transform, and write data. Azure Synapse provides a scalable, optimized platform for performing such tasks efficiently. Explore its features further to unlock the full potential of big data processing in the cloud.