Spark Lessons LogoSpark Lessons
articleReference Guide

PySpark SQL Cheatsheet

Quick reference guide for PySpark SQL, DataFrame APIs, and structured data query operations.

picture_as_pdfDownload PDF
search
powerInitializing SparkSession

Initialize a SparkSession, the primary entry point for working with structured data (DataFrames and SQL) in Spark.

downloadCreating DataFrames

Create a DataFrame from an RDD by map-splitting lines and applying a Row schema structure (inferring schema).

settingsCreating DataFrames

Create a DataFrame from an RDD by programmatically specifying a Schema using StructType and StructField classes.

folder_openCreating DataFrames

Read and load structured datasets from external files (JSON, Parquet, Text) into DataFrames.

infoInspecting Data

Inspect basic attributes, view records, and check schema types of the DataFrame.

analyticsInspecting Data

Retrieve summary statistics, count records, verify schema tree structure, and print execution query plans.

searchQueries & Selection

Project columns from the DataFrame. Supports basic column selecting and nested field expansions using explode().

calculateQueries & Selection

Select columns and evaluate mathematical increments or boolean checks.

ruleQueries & Selection

Evaluate condition checks inside queries, generating conditional outcomes using when-otherwise logic.

filter_altQueries & Selection

Keep rows matching a boolean criteria or match elements belonging to a specific list of values.

titleQueries & Selection

Query columns using text operations (regex-like, prefix/suffix searches, or substring slicing).

compare_arrowsQueries & Selection

Filter or select values that fall within a lower and upper range boundaries (inclusive).

add_circleModifying Columns

Add new columns or replace existing columns in the DataFrame with computed expressions.

editModifying Columns

Rename a column to a new target name, returning a new DataFrame projection.

deleteModifying Columns

Remove specified columns from the DataFrame by name or reference.

done_allModifying Columns

Deduplicate records in the DataFrame, returning only unique rows.

group_workGrouping & Aggregation

Group DataFrame rows by columns, allowing aggregations (like counting items per group).

sortSorting & Ordering

Sort and order records by one or multiple columns, in ascending or descending sequence.

find_replaceHandling Nulls & Missing

Handle missing datasets by filling defaults, dropping null rows, or swapping target values.

table_chartSQL Queries & Views

Register DataFrames as temporary tables, allowing SQL queries to be executed over them.

terminalSQL Queries & Views

Execute SQL query strings programmatically, returning the query results as a new DataFrame.

swap_horizData Formats & Output

Convert DataFrames into raw RDDs, JSON records arrays, or local Pandas DataFrames.

grid_onSorting & Partitions

Manage partition counts across the cluster. repartition() performs full shuffles; coalesce() merges partitions without a shuffle.

saveData Formats & Output

Save DataFrame contents to target storage formats (Parquet, JSON, etc.) at path.

stop_circleSpark Lifecycle

Close the SparkSession instance. Releases active context connections and cluster executor memory.