Spark List Function Example
Spark List Function Example
Below is a comprehensive list of commonly used PySpark functions categorized by their purpose,
along with examples to help you prepare for your interview:
df = [Link]("Country", lit("USA"))
[Link](col("name").alias("full_name")).show()
Aggregations
[Link]("category").agg(sum("sales").alias("total_sales")).show()
[Link]("category").count().show()
[Link](sum("sales").alias("total_sales"),
[Link] 1/13
01/01/2025, 01:27 Missing Columns Debugging
avg("sales").alias("average_sales")).show()
[Link]("column_name").distinct().show()
String Operations
[Link](col("name").substr(1, 5).alias("short_name")).show()
df = [Link]("lower_name", lower(col("name")))
df = [Link]("today", current_date())
[Link] 2/13
01/01/2025, 01:27 Missing Columns Debugging
df = [Link]("year", year(col("start_date")))
Null Handling
df = [Link](subset=["column_name"])
[Link](col("name").isNull()).show()
Window Functions
Filtering
[Link] 3/13
01/01/2025, 01:27 Missing Columns Debugging
[Link](col("age").between(20, 30)).show()
Joins
[Link]().show()
[Link](5)
print([Link]())
print([Link])
[Link]()
Actions
[Link] 4/13
01/01/2025, 01:27 Missing Columns Debugging
rows = [Link]()
print([Link](5))
print([Link]())
pdf = [Link]()
[Link]()
[Link] 5/13
01/01/2025, 01:27 Missing Columns Debugging
This list covers a broad range of PySpark functions and scenarios. Let me know if you'd like a deeper
dive into any specific area!
Here's a refined and complete list of PySpark functions and concepts for your interview preparatio
ensured no key functions or concepts are missed:
1. DataFrame Creation
2. Data Exploration
[Link](True)
[Link]().show()
3. Column Manipulation
[Link] 6/13
01/01/2025, 01:27 Missing Columns Debugging
[Link]("Name", "Age").show()
df = [Link]("Age")
df = [Link]("Name", "FullName")
[Link](col("Name").isin(["Alice", "Bob"])).show()
[Link](col("Name").like("%A%")).show()
[Link](col("Name").rlike("^A.*")).show()
5. Aggregations
[Link]("category").pivot("month").sum("sales").show()
python
[Link] 7/13
01/01/2025, 01:27 Missing Columns Debugging
Copy cod
[Link]("category", "sub_category").sum("sales").show()
[Link](approx_count_distinct("column_name")).show()
[Link]("category").agg(collect_list("values")).show()
6. Data Cleaning
7. Joins
8. String Functions
df = [Link]("capitalized", initcap(col("name")))
[Link] 8/13
01/01/2025, 01:27 Missing Columns Debugging
9. Date and Time Functions
df = [Link](10)
df = [Link](1)
df = [Link]()
11. Miscellaneous
df = [Link](col("Age").desc())
[Link](fraction=0.1).show()
df = [Link]("element", explode(col("array_column")))
[Link] 9/13
01/01/2025, 01:27 Missing Columns Debugging
Pro Tip
1. Understand Catalyst Optimizer: Mention how PySpark optimizes queries using DAGs (Directe
Acyclic Graphs).
Broadcast joins.
Let me know if you'd like additional details or examples for any function!
### **Pro Tip** 1. **Understand Catalyst Optimizer**: Mention how PySpark optimizes queries using DAGs
(Directed Acyclic Graphs). 2. **Mention Use Cases**: For example: * Using PySpark for ETL. * Working with big dat
in distributed environments. 3. **Discuss Performance**: Mention strategies like: * **Broadcast joins**. *…
The Catalyst Optimizer in PySpark is an internal query optimizer for transforming logical query pl
into optimized physical query plans. It uses rule-based and cost-based optimization to improve
query performance.
Key Points:
[Link] 10/13
01/01/2025, 01:27 Missing Columns Debugging
Automatically applies techniques like predicate pushdown, column pruning, and join
optimization.
Converts transformations into a Directed Acyclic Graph (DAG) for efficient execution.
Example:
PySpark is widely used in real-world applications for big data processing. Be prepared to discuss
scenarios where PySpark can solve problems.
1. ETL with PySpark: PySpark can extract data from sources like S3 or HDFS, transform it, and lo
it into a target database.
2. Big Data Processing: PySpark handles distributed processing of large datasets, making it idea
for batch processing or real-time streaming.
3. Discuss Performance
PySpark provides several techniques to optimize the performance of large-scale data processing.
a. Broadcast Joins
Use broadcast joins when one of the datasets is small enough to fit into memory. This reduces the
data shuffle during joins.
[Link] 11/13
01/01/2025, 01:27 Missing Columns Debugging
repartition : Increases or decreases the number of partitions. It involves a full shuffle of dat
coalesce : Reduces the number of partitions without a full shuffle, which is faster.
persist : Allows storing in memory and disk (or other storage levels).
When to Use:
Use cache or persist when a DataFrame is reused multiple times to avoid recomputation.
Discuss scenarios where PySpark improved processing time or handled large datasets
efficiently.
[Link] 12/13
01/01/2025, 01:27 Missing Columns Debugging
[Link] 13/13