PySpark DataFrame summary() for String and Numeric Columns
Short answer
A Using df.summary() meets the goal of calculating summary statistics for all string and numeric columns.
Question
Key Takeaways
The question tests knowledge of PySpark's df.summary() function, specifically that it operates on both string and numeric columns, unlike pandas' default describe().
This page confirms that the PySpark df.summary() method correctly calculates summary statistics for both string and numeric columns. It establishes that the method meets the goal of evaluating data for min, max, mean, and standard deviation.
Common mistake: Choosing 'No' because mean and standard deviation are not applicable to string columns, failing to realize df.summary() still includes string columns and computes applicable statistics like min and max.
Expert Analysis
Why the Answer Is Correct
The df.summary method in PySpark is explicitly designed to compute summary statistics for both numeric and string columns. For numeric columns, it calculates count, mean, stddev, min, max, and optional percentiles. For string columns, it calculates count, min, and max, returning null for inapplicable statistics like mean and stddev. Therefore, using df.summary successfully meets the goal of evaluating the data across all string and numeric columns.
Why the Other Options Are Wrong
Option B suggests the goal is not met, which is incorrect. A common misconception is that df.summary ignores string columns entirely or fails if it cannot compute mean and stddev for them. While those specific statistical measures are undefined for strings, the method still processes string columns to find applicable statistics like min and max, fully satisfying the requirement to evaluate all column types.
Community Comment Notes
Several commenters correctly noted that df.summary works on both string and numeric columns, with one stating "In PySpark, use df.summary for summary statistics of both numeric and string columns". Others argued against it because "mean and standard deviation are not applicable to string data", but the function gracefully handles this by returning null for those metrics while still evaluating the string columns. Another commenter incorrectly claimed "df.summary.show is correct", but the calculation occurs within the summary method itself.
Official Reference
This link opens the official vendor documentation in a new tab.
Exam Strategy
Remember that PySpark's df.summary() and df.describe() process both string and numeric columns, unlike pandas which defaults to numeric only. Do not assume the function fails on string columns just because some statistics are undefined.
Community Votes
55% of anonymous learners picked answer A. Votes are pick records left by other test-takers — they are not the verified answer.
Discussion 11 contributions
Compiled from public community threads and reader submissions, lightly edited for clarity. Handles and vote tallies are shown as recorded, and comments reflect the views of individual contributors.
From public community threads 11
-
stilferx 👍 9IMHO, A Example: df1 = spark.createDataFrame([(1, 10), (2, 10), (2, 15)], schema = ['fruit_id', 'amount']) df1.summary() summary fruit_id amount count 3 3 mean 1.6666666666666667 11.666666666666666 stddev 0.5773502691896257 2.886751345948129 min 1 10 25% 1 10 50% 2 10 75% 2 15 max 2 15
-
282b85d 👍 4 Answered Bwhile df.summary() does provide valuable information for numeric columns, it does not fully meet the goal of evaluating both string and numeric columns with the required statistical measures. Use df.summary() and df.agg() to cover numeric columns, and additional custom aggregations for string columns.
-
SamuComqi 👍 4 Answered AAlso df.describe() is a valid solution. Sources: summary --> https://spark.apache.org/docs/latest/api/python/reference/pyspark.sql/api/pyspark.sql.DataFrame.summary.html describe --> https://spark.apache.org/docs/latest/api/python/reference/pyspark.sql/api/pyspark.sql.DataFrame.describe.html
-
7d97b62 👍 3 Answered AIn pandas, use df.describe() for summary statistics of numeric columns. In PySpark, use df.summary() for summary statistics of both numeric and string columns in a distributed computing environment.
-
Pegooli 👍 2 Answered BUsing df.summary() in PySpark will provide summary statistics, including min, max, mean, and standard deviation for all numeric columns. However, it will not provide these statistics for string columns since summary statistics like min, max, mean, and standard deviation are not applicable to string data.
Show 6 more comments
-
2fe10ed 👍 1 Answered Ahttps://spark.apache.org/docs/latest/api/python/reference/pyspark.sql/api/pyspark.sql.DataFrame.summary.html#pyspark.sql.DataFrame.summary
-
b01d700 👍 1 Answered BThe correct PySpark expression to calculate min, max, mean, and standard deviation for both numeric and STRING columns is: df.describe()
-
Lotusss 👍 1 Answered Bdf.summary() alone wont cut it. df.summary().show() is correct. So anwser is B
-
XiltroX 👍 1df.summary() is the only option where you can get MIX, MAX and AVG
-
slu239 👍 1 Answered BNot meet the goal because it has to be df.summary().show()
-
6d1de25 👍 1 Answered ACorrect
Reader comments & corrections
No reader corrections yet — spotted an error or have a note? Share it below.
Related Analysis
Practice All DP-600 Questions
Access 115 questions with complete answers and detailed explanations.
View Full DP-600 Practice Test →