Skip to content
PHP2Shell: The PHP Bug
Akshay Thakare

PySpark DataFrame Basics in 10 Operations

The ten DataFrame operations that cover most pipeline code: read, select, filter, withColumn, join, groupBy, window, dedupe, union and write.

PySparkBeginner2 min readUpdated 11 Oct 2026
Python.py
from pyspark.sql import SparkSession, functions as F, Window
 
spark = SparkSession.builder.appName("orders").getOrCreate()

1–3. Read, select, filter

Python.py
orders = spark.read.parquet("s3://lake/raw/orders/")
paid = orders.select("order_id", "store_id", "amount", "status", "ts").filter(F.col("status") == "paid")

4. Add or change a column

Python.py
paid = paid.withColumn("business_date", F.to_date("ts"))

5. Join

Python.py
stores = spark.read.parquet("s3://lake/dim/stores/")
enriched = paid.join(stores, "store_id", "left")

6. Aggregate

Python.py
daily = enriched.groupBy("business_date", "region").agg(
    F.sum("amount").alias("net_sales"),
    F.countDistinct("order_id").alias("orders"),
)

7. Window

Python.py
w = Window.partitionBy("region").orderBy("business_date")
daily = daily.withColumn("running_sales", F.sum("net_sales").over(w))

8. Deduplicate (latest per key)

Python.py
w = Window.partitionBy("order_id").orderBy(F.col("ts").desc())
latest = orders.withColumn("rn", F.row_number().over(w)).filter("rn = 1").drop("rn")

dropDuplicates(["order_id"]) keeps an arbitrary row. Use the window when "latest" matters.

9. Union

Python.py
both = jan.unionByName(feb, allowMissingColumns=True)   # by name, not position

10. Write

Python.py
daily.write.mode("overwrite").partitionBy("business_date").parquet("s3://lake/curated/daily/")

Finished this note?

Share