TGViewer
Data Engineers Data Engineers @sql_engineer · 11.2K subscribers
Post #355 1.19K
Two Commonly Asked Pyspark Inrerview Questions!!:


Scenario 1: Handling Missing Values


Interviewer: "How would you handle missing values in a PySpark DataFrame?"


Candidate:


from pyspark.sql.functions import when, isnan

# Load the DataFrame
df = spark.read.csv("path/to/data.csv", header=True, inferSchema=True)

# Check for missing values
missing_count = df.select([count(when(isnan(c), c)).alias(c) for c in df.columns])

# Replace missing values with mean
from pyspark.sql.functions import mean
mean_values = df.agg(*[mean(c).alias(c) for c in df.columns])
df_filled = df.fillna(mean_values)

# Save the cleaned DataFrame
df_filled.write.csv("path/to/cleaned/data.csv", header=True)


Interviewer: "That's correct! Can you explain why you used the fillna() method?"


Candidate: "Yes, fillna() replaces missing values with the specified value, in this case, the mean of each column."


*Scenario 2: Data Aggregation*


Interviewer: "How would you aggregate data by category and calculate the average sales amount?"


Candidate:


# Load the DataFrame
df = spark.read.csv("path/to/data.csv", header=True, inferSchema=True)

# Aggregate data by category
from pyspark.sql.functions import avg
df_aggregated = df.groupBy("category").agg(avg("sales").alias("avg_sales"))

# Sort the results
df_aggregated_sorted = df_aggregated.orderBy("avg_sales", ascending=False)

# Save the aggregated DataFrame
df_aggregated_sorted.write.csv("path/to/aggregated/data.csv", header=True)


Interviewer: "Great answer! Can you explain why you used the groupBy() method?"


Candidate: "Yes, groupBy() groups the data by the specified column, in this case, 'category', allowing us to perform aggregation operations."
  • 👍 1
More from @sql_engineer
  1. Aug 29, 2026Example: Source Database → CDC → Only Changed Records → Data Platform CDC is especially us…
  2. Aug 29, 2026🚀 Data Engineering Fundamentals – Part 7 📥 Data Ingestion: How Data Enters a Data Platfo…
  3. Aug 18, 2026🚀 Data Engineering Fundamentals – Part 6 📌 ETL vs ELT: How Data Moves from Source to Des…
  4. Aug 11, 2026📊 The 90-Minutes Business Analytics Masterclass Learn how to transform raw data into powe…
  5. Aug 8, 2026Data Warehouse Stores: Cleaned sales data Customer KPIs Revenue reports Historical busines…
  6. Aug 8, 2026🚀 Data Engineering Fundamentals – Part 4 📌 Databases vs Data Warehouses vs Data Lakes vs…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →