PySpark Transformations

0.0(0)
Studied by 0 people
call kaiCall Kai
Locked
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/25

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 2:32 PM on 7/29/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

26 Terms

1
New cards

df.select("col") or df.select(col("col"))

Selects specific columns. Accepts strings, col() objects, or expressions. Equivalent to SQL SELECT.

2
New cards

df.filter(condition) / df.where(condition)

Filters rows. where() is an alias for filter(). df.filter(col("age") > 18). Equivalent to SQL WHERE.

3
New cards

df.withColumn("name", expr)

Adds a new column or replaces an existing one. df.withColumn("total", col("price") * col("qty"))

4
New cards

df.withColumnRenamed("old", "new")

Renames a column. Does not modify other columns.

5
New cards

df.drop("col1", "col2")

Removes one or more columns from the DataFrame.

6
New cards

df.groupBy("col").agg(…)

Groups rows and applies aggregate functions. df.groupBy("dept").agg(F.sum("sal").alias("total_sal"))

7
New cards

df.orderBy(col("col").desc())

Sorts the DataFrame. Use .asc() or .desc() on a col(). orderBy() and sort() are aliases.

8
New cards

df.join(df2, on="col", how="inner")

Joins two DataFrames. how options: "inner", "left", "right", "outer", "left_semi", "left_anti", "cross"

9
New cards

left_semi join

Returns rows from the LEFT DataFrame that have a match in the right. No columns from right table. Like SQL EXISTS.

10
New cards

left_anti join

Returns rows from the LEFT DataFrame that have NO match in the right. Like SQL NOT EXISTS.

11
New cards

df.distinct()

Removes duplicate rows across all columns. Equivalent to SQL SELECT DISTINCT *.

12
New cards

df.dropDuplicates(["col1", "col2"])

Removes duplicates based on specific columns. More targeted than distinct().

13
New cards

df.limit(n)

Returns a new DataFrame with the first n rows. Lazy — only executes on action.

14
New cards

df.union(df2)

Combines two DataFrames with the same schema by position. Keeps duplicates.

15
New cards

df.unionByName(df2)

Combines two DataFrames matching columns by name, not position. Safer for reordered schemas.

16
New cards

df.alias("a")

Assigns an alias to a DataFrame. Used to disambiguate columns in self-joins: df.alias("a").join(df.alias("b"), …)

17
New cards

col("x").cast("integer")

Casts a column to a new type inside an expression. df.withColumn("x", col("x").cast("integer"))

18
New cards

df.na.drop(how, subset)

Drops rows with NULLs. how="any" (default) drops if any null; how="all" drops only if all nulls. subset limits to specific columns.

19
New cards

df.na.fill(value, subset)

Fills NULLs. df.na.fill(0) for numeric, or df.na.fill({"col1": 0, "col2": "N/A"}) for per-column.

20
New cards

df.repartition(n, col)

Shuffles data into n partitions (full shuffle). Use to increase partitions or repartition by a key column.

21
New cards

df.coalesce(n)

Reduces partitions without a full shuffle. Efficient for decreasing partition count before writing.

22
New cards

df.explode(col)

Expands an array or map column into multiple rows — one row per element.

23
New cards

df.pivot("col")

Rotates row values into columns. df.groupBy("year").pivot("month").agg(F.sum("sales"))

24
New cards

df.sample(fraction, seed)

Returns a random sample of rows. df.sample(0.1, seed=42) = 10% sample.

25
New cards

Window.partitionBy("col").orderBy("col")

Defines a window specification for window functions. w = Window.partitionBy("dept").orderBy("sal")

26
New cards

df.withColumn("rn", F.row_number().over(w))

Applies a window function. Must use .over(windowSpec). Requires importing Window from pyspark.sql.window.