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
from pyspark.sql import SparkSession, functions as F, Window
spark = SparkSession.builder.appName("orders").getOrCreate()1–3. Read, select, filter
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
paid = paid.withColumn("business_date", F.to_date("ts"))5. Join
stores = spark.read.parquet("s3://lake/dim/stores/")
enriched = paid.join(stores, "store_id", "left")6. Aggregate
daily = enriched.groupBy("business_date", "region").agg(
F.sum("amount").alias("net_sales"),
F.countDistinct("order_id").alias("orders"),
)7. Window
w = Window.partitionBy("region").orderBy("business_date")
daily = daily.withColumn("running_sales", F.sum("net_sales").over(w))8. Deduplicate (latest per key)
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
both = jan.unionByName(feb, allowMissingColumns=True) # by name, not position10. Write
daily.write.mode("overwrite").partitionBy("business_date").parquet("s3://lake/curated/daily/")Finished this note?
