Provide PySpark code to compute the acceptance rate per date based on the given table and code.
1Times asked
Apr 2026Last seen
Apr 2026First seen
π‘ Model Answer
Here is a concise PySpark solution:
python
from pyspark.sql import functions as F
# Assume df is the original DataFrame with columns: date, action
sent = df.filter(F.col("action") == "sent")
.groupBy("date")
.agg(F.count("*" ).alias("sent_cnt"))
accepted = df.filter(F.col("action") == "accepted")
.groupBy("date")
.agg(F.count("*" ).alias("accepted_cnt"))
# Join the two aggregates on date
rate_df = sent.join(accepted, "date", "inner")
.withColumn("acceptance_rate", (F.col("accepted_cnt") / F.col("sent_cnt")) * 100)
.select("date", "acceptance_rate")
rate_df.show()This code groups by date, counts sent and accepted actions, joins the counts, and calculates the percentage. Complexity is O(n) for the filters and groupBy, and O(m) for the join, where n is the number of rows and m is the number of distinct dates.
This answer was generated by AI for study purposes. Use it as a starting point β personalize it with your own experience.
π€ Get questions like this answered in real-time
Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers β invisible to screen sharing.
Get Assisting AI β Starts at βΉ500