| transaction_id | customer_id | product | quantity | unit_price | revenue | |
|---|---|---|---|---|---|---|
| 0 | 1 | 101 | Laptop | 1 | 900 | 900 |
| 1 | 2 | 102 | Phone | 2 | 500 | 1000 |
| 2 | 3 | 101 | Mouse | 3 | 20 | 60 |
| 3 | 4 | 103 | Laptop | 1 | 900 | 900 |
| Transaction ID | Customer ID | Product ID | Quantity | Unit price | Transaction date |
|---|---|---|---|---|---|
| 0001 | 101 | Laptop | 1 | 900 | 2024-01-01 |
| \(\dots\) | \(\dots\) | \(\dots\) | \(\dots\) | \(\dots\) | \(\dots\) |
| transaction_id | customer_id | product | quantity | unit_price | revenue | |
|---|---|---|---|---|---|---|
| 0 | 1 | 101 | Laptop | 1 | 900 | 900 |
| 1 | 2 | 102 | Phone | 2 | 500 | 1000 |
| 2 | 3 | 101 | Mouse | 3 | 20 | 60 |
| 3 | 4 | 103 | Laptop | 1 | 900 | 900 |



| Feature | Pandas |
PySpark ![]() |
Dask |
|---|---|---|---|
| Main purpose | Data analysis | Distributed processing (JVM) | Parallel computing (Python) |
| Execution | Local | Distributed or local | Local or distributed |
| API | Pandas | Similar to SQL/PD | Similar to Pandas |
| Lazy execution | No | Yes | Yes |
| Large datasets | Limited by local resources | Designed for large-scale data | Designed for parallel computing |
| Ecosystem | Python | Apache Spark | Python |
Pandas and SQL.Java runtime. Make sure Java is installed and available in your environment.Important: For this introduction, we will run Spark locally on our computer. The same programming concepts can later be used on a Spark cluster.
SparkSession.builder: entry point to configure a Spark session.appName(): name of your application/session.master("local[*]"): run locally using all available CPU cores.getOrCreate(): create or reuse a Spark session.Intuition: Pandas works directly with local DataFrames. PySpark uses a Spark session to coordinate data processing using JVM-based executors.
We can create a Spark DataFrame from the same data used in Pandas.
sales = [
(1, 101, "Laptop", 1, 900),
(2, 102, "Phone", 2, 500),
(3, 101, "Mouse", 3, 20),
(4, 103, "Laptop", 1, 900),
(5, 102, "Phone", 1, 500)
]
columns = [
"transaction_id",
"customer_id",
"product",
"quantity",
"unit_price"
]
sdf = spark.createDataFrame(sales, columns)
sdf.show() # we show the data+--------------+-----------+-------+--------+----------+
|transaction_id|customer_id|product|quantity|unit_price|
+--------------+-----------+-------+--------+----------+
| 1| 101| Laptop| 1| 900|
| 2| 102| Phone| 2| 500|
| 3| 101| Mouse| 3| 20|
| 4| 103| Laptop| 1| 900|
| 5| 102| Phone| 1| 500|
+--------------+-----------+-------+--------+----------+
Notice that we use show() rather than simply writing the DataFrame name as we often do in Pandas.
Pyspark execution engine includes its own SQL and query optimizers, DAG scheduler, task scheduler, shuffle manager, etc.count() is an action in Spark. It triggers computation to obtain the number of rows.<class 'pandas.core.frame.DataFrame'>
RangeIndex: 4 entries, 0 to 3
Data columns (total 6 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 transaction_id 4 non-null int64
1 customer_id 4 non-null int64
2 product 4 non-null object
3 quantity 4 non-null int64
4 unit_price 4 non-null int64
5 revenue 4 non-null int64
dtypes: int64(5), object(1)
memory usage: 324.0+ bytes
We often start by selecting the variables relevant to our analysis.
Pandas
The select() operation in PySpark is similar to selecting columns in Pandas.
Suppose we only want transactions with a unit_price > 101.
| transaction_id | customer_id | product | quantity | unit_price | revenue | |
|---|---|---|---|---|---|---|
| 0 | 1 | 101 | Laptop | 1 | 900 | 900 |
| 1 | 2 | 102 | Phone | 2 | 500 | 1000 |
| 3 | 4 | 103 | Laptop | 1 | 900 | 900 |
+--------------+-----------+-------+--------+----------+
|transaction_id|customer_id|product|quantity|unit_price|
+--------------+-----------+-------+--------+----------+
| 1| 101| Laptop| 1| 900|
| 2| 102| Phone| 2| 500|
| 4| 103| Laptop| 1| 900|
| 5| 102| Phone| 1| 500|
+--------------+-----------+-------+--------+----------+
We can also use where() as an alternative to filter().
revenue of each transaction: \[\text{Revenue} = \text{Quantity} \times \text{Unit Price}\]Pandas
withColumn() creates a new column or replaces an existing one.PySpark
+--------------+-----------+-------+--------+----------+-------+
|transaction_id|customer_id|product|quantity|unit_price|revenue|
+--------------+-----------+-------+--------+----------+-------+
| 1| 101| Laptop| 1| 900| 900|
| 2| 102| Phone| 2| 500| 1000|
| 3| 101| Mouse| 3| 20| 60|
| 4| 103| Laptop| 1| 900| 900|
| 5| 102| Phone| 1| 500| 500|
+--------------+-----------+-------+--------+----------+-------+
Business question: Which products generate the highest revenue?
Pandas
product
Laptop 1800
Phone 1000
Mouse 60
Name: revenue, dtype: int64
Pandas: group observations and calculate summary statistics. Spark distributes the partitions across workers.PySpark
+-------+-------------+---------------+------------+
|product|total_revenue|average_revenue|transactions|
+-------+-------------+---------------+------------+
| Laptop| 1800| 900.0| 2|
| Phone| 1500| 750.0| 2|
| Mouse| 60| 60.0| 1|
+-------+-------------+---------------+------------+
customers = [
(101, "Alice", "France"),
(102, "Bob", "Cambodia"),
(103, "Charlie", "France")
]
customer_columns = [
"customer_id", "customer_name", "country"
]
customer_pdf = pd.DataFrame(
{
"customer_id" : (101, 102, 103),
"customer_name" : ("Alice", "Bob", "Charlie"),
"country" : ("France", "Cambodia", "France")
}
)
customer_sdf = spark.createDataFrame(
customers,
customer_columns
)+-----------+--------------+-------+--------+----------+-------+-------------+--------+
|customer_id|transaction_id|product|quantity|unit_price|revenue|customer_name| country|
+-----------+--------------+-------+--------+----------+-------+-------------+--------+
| 101| 1| Laptop| 1| 900| 900| Alice| France|
| 102| 2| Phone| 2| 500| 1000| Bob|Cambodia|
| 101| 3| Mouse| 3| 20| 60| Alice| France|
+-----------+--------------+-------+--------+----------+-------+-------------+--------+
only showing top 3 rows
Spark also allows us to write SQL queries directly.DataFrame as a temporary SQL table.SQL:Spark has optimizers that optimize the execution plan before running the query in SQL.One important difference between Pandas and Spark is lazy evaluation.
Pandas
The computation is performed when the operation is executed.
select(), filter(), withColumn(), groupBy(), join()…show(), count(), collect(), write()…Spark can optimize a sequence of transformations before executing it.
== Physical Plan ==
AdaptiveSparkPlan isFinalPlan=false
+- HashAggregate(keys=[product#2], functions=[sum(quantity#3L)])
+- Exchange hashpartitioning(product#2, 200), ENSURE_REQUIREMENTS, [plan_id=321]
+- HashAggregate(keys=[product#2], functions=[partial_sum(quantity#3L)])
+- Project [product#2, quantity#3L]
+- Filter (isnotnull(quantity#3L) AND (quantity#3L > 1))
+- Scan ExistingRDD[transaction_id#0L,customer_id#1L,product#2,quantity#3L,unit_price#4L]
explain() displays the execution plan. It helps us understand how Spark intends to process the data.Spark does not necessarily execute every operation immediately. It can optimize the overall computation plan before running it.+-------+-------------+
|product|total_revenue|
+-------+-------------+
| Laptop| 1800|
| Phone| 1500|
| Mouse| 60|
+-------+-------------+
Load → Transform → Aggregate → Sort → Save.
Dask is a Python library for parallel and distributed computing.Python libraries.Dask DataFrame is designed for working with larger-than-memory tabular datasets.Dask aims to make parallel computing more accessible to Python users, particularly those already familiar with including Pandas, NumPy, Scikit-learn…Load the data:
Notice the similarity with Pandas:
Python frameworks like NumPy, Pandas, Scikit-learn, etc.Python.Question: Calculate the total revenue by product.
Pandas
.compute() triggers the computation and returns the result. It’s also perform lazy execution.| Concept | Pandas | Dask | PySpark |
|---|---|---|---|
| DataFrame | Local | Partitioned | Distributed |
| Execution | Eager | Lazy | Lazy |
| Parallelism | Limited | Yes | Yes |
| Main interface | Python | Pandas-like | SQL/DataFrame |
| Typical use | Small/medium data | Parallel Python workflows | Large-scale distributed analytics |
Dask is not simply a replacement for Pandas or Spark. The appropriate framework depends on the dataset, available infrastructure, and type of computation…| Situation | Suggested tool |
|---|---|
| Small dataset fitting comfortably in memory | Pandas |
| Local parallel computing with Python | Dask |
| Large-scale distributed data processing | PySpark |
| SQL-based distributed analytics | PySpark |
Spark and Dask can also run locally.
- The goal of Big Data technology is not to change how we think about data analysis.
- It is to allow familiar analytical operations to work at a much larger scale.
- Choosing the right framework would allow smooth and efficient data analysis at scale.
🥳 Yeahhhh 🥂!!!
Let’s take a break!
