Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 31

Descriptive Statistics with Pandas

Recommended resources

Hello! Welcome to your next lesson in our journey through probability and statistics.

In our last session, we explored the theoretical properties of key continuous distributions like the Uniform, Exponential, and Normal distributions. We discussed their parameters, such as the theoretical mean () and variance (). Today, we'll bridge the gap from theory to practice. You'll learn how to take a real dataset and calculate these essential summary numbers yourself. This process is a cornerstone of Exploratory Data Analysis (EDA).

Our learning outcome for this lesson is to compute and interpret descriptive statistics (mean, median, variance) for a dataset using Pandas. We will focus on two main types of descriptive statistics:

  • Measures of Central Tendency: These tell us about the "center" or "typical" value of the data (mean, median).
  • Measures of Dispersion: These tell us how "spread out" or "varied" the data is (variance, standard deviation).

Mastering these skills is crucial for the GATE exam, as they are the first step in understanding and summarizing any dataset you encounter.

1. The Building Blocks of Data Summary

Descriptive statistics provide a simple summary of our data. Before we dive into the "how," let's get a formal introduction to these concepts.

Interpreting Data Using Descriptive Statistics with Python

The article 'Interpreting Data Using Descriptive Statistics with Python' from Pluralsight gives a great overview of the concepts we'll cover. We will use it as a reference throughout this lesson.

Please read the 'Introduction' and 'Data' sections. This will introduce the main statistical measures and the loan applicant dataset we'll refer to.


2. Measures of Central Tendency: Finding the "Middle"

When you have a set of numbers, what is the most representative value? The answer isn't always straightforward. We'll look at two primary measures: the mean and the median.

The Mean (Average)

The mean is the most common measure of center. It's simply the sum of all values divided by the count of values. In our previous lesson, we called this the expected value for a theoretical distribution. When we calculate it from data, it's called the sample mean.

The Median (The Middle Value)

The median is the value that sits right in the middle of your data when it's sorted. 50% of the data is below the median, and 50% is above it.

Mean vs. Median: The Problem of Outliers

So why do we need both? The mean is very sensitive to extreme values, or outliers. The median is not. This makes the median a more robust statistic. Let's watch a video that explains this critical difference with a brilliant example.

Median, Mean, Mode, Percentile | Math, Statistics for data science, machine learning

The video 'Median, Mean, Mode, Percentile' from the codebasics channel provides an excellent, intuitive explanation for why the median is often a better measure than the mean when outliers are present.

Watch from the beginning to 04:50. Pay close attention to the car showroom example with Elon Musk. This perfectly illustrates how a single outlier can dramatically skew the mean, while the median remains a better representation of the typical income.

Calculating Mean and Median in Pandas

Now that you understand the concepts, let's see how easy it is to calculate these in Python using Pandas. We'll load a dataset and apply the built-in functions.

Python for Data Analysis: Descriptive Statistics

The video 'Python for Data Analysis: Descriptive Statistics' by DataDaft demonstrates the practical steps for calculating these measures on a Pandas DataFrame.

Watch from the beginning to 04:24. Focus on how the .mean() and .median() functions are used. The video also reinforces the concept of how skewed data affects the mean more than the median, just as we saw with the outlier example.

To summarize, you can calculate the mean and median for a column (a Pandas Series) or for an entire DataFrame:

# Assuming 'df' is your DataFrame and 'column_name' is the column you're interested in
mean_value = df['column_name'].mean()
median_value = df['column_name'].median()

# You can also apply it to all numeric columns at once
all_means = df.mean()
all_medians = df.median()
Test your understanding!

You are analyzing a dataset of house prices in a city. The dataset includes prices for regular family homes and also for a few multi-million dollar celebrity mansions.

Which measure of central tendency (mean or median) would give you a more realistic idea of the price of a typical house? Why?

Show answer

The median would be more appropriate. The multi-million dollar mansions are outliers. They will significantly pull the mean upwards, making it seem like the typical house is much more expensive than it actually is. The median, being the middle value, is not affected by these extreme values and will give a more accurate representation of a typical home price.


3. Measures of Dispersion: Gauging the Spread

Knowing the center of the data isn't enough. We also need to know how spread out the data points are. Are they all clustered tightly around the mean, or are they all over the place? This is measured by variance and standard deviation.

  • Variance: This is the average of the squared differences from the mean. A larger variance means the data is more spread out. The formula is .
  • Standard Deviation: This is simply the square root of the variance (). We often prefer it because it's in the same units as the original data, making it easier to interpret. For example, if you're measuring heights in centimeters, the standard deviation will also be in centimeters, while the variance would be in "centimeters squared".

Let's see how to calculate and interpret these measures.

Python for Data Analysis: Descriptive Statistics

Let's return to the DataDaft video to see how variance and standard deviation are calculated in Pandas.

Watch from 09:21 to 10:55. Notice the clear explanation of what variance is and why standard deviation is often more interpretable. The Pandas functions are .var() and .std().

The Pandas functions are just as straightforward:

# Calculate variance and standard deviation for a column
variance_value = df['column_name'].var()
std_dev_value = df['column_name'].std()

4. Putting It All Together: The .describe() Method

Calculating each of these statistics one by one is useful, but for a quick overview, Pandas provides a wonderfully convenient method: .describe(). This single command gives you a summary of the key descriptive statistics for all numerical columns in your DataFrame.

Interpreting Data Using Descriptive Statistics with Python

Now, let's look at a powerful shortcut. The Pluralsight article explains how the .describe() function can summarize our data in one go.

Read the section 'Putting Everything Together'. Study the output table from df.describe(). Note that it includes the count, mean, standard deviation, min/max values, and quartiles (25%, 50%, 75%). The 50th percentile is the median!

The .describe() method is one of the first things you'll run when you get a new dataset. It provides a wealth of information at a glance.

Pandas describe() method illustration
This image shows exactly what the `.describe()` method does. It takes a DataFrame as input and produces a summary table with key statistics for each numerical column, making it an incredibly efficient tool for initial data analysis.
Test your understanding!

You run df['age'].describe() and get the following output:

count    100.0
mean      35.0
std       10.0
min       20.0
25%       28.0
50%       34.0
75%       42.0
max       70.0
  1. What is the median age in this dataset?
  2. What does the std value of 10.0 tell you about the ages in this dataset?
Show answer
  1. The median age is the 50th percentile (50%), which is 34.0.
  2. A standard deviation (std) of 10.0 indicates the typical spread or variation of ages around the mean age of 35. Loosely speaking, a "typical" data point is about 10 years away from the average age. A smaller standard deviation would imply that most people's ages are very close to 35, while a larger one would imply the ages are more spread out.

Conclusion

Excellent work! You've taken a major step from understanding theoretical probability to performing practical data analysis. You are now equipped to take any numerical dataset and generate a meaningful statistical summary.

Key Takeaways:

  • Descriptive statistics summarize the main features of a dataset.
  • Measures of Central Tendency like mean (average) and median (middle value) describe the "center" of the data. The median is more robust to outliers.
  • Measures of Dispersion like variance and standard deviation describe the "spread" of the data. Standard deviation is often preferred for interpretation as it's in the original units.
  • Pandas makes calculation easy with .mean(), .median(), .var(), and .std().
  • The .describe() method is a powerful tool for getting a quick, comprehensive statistical summary of your numerical data.

Preview of the Next Lesson:

Reading numbers from a table is useful, but a picture is often worth a thousand words. In our next lesson, you will learn how to create and interpret histograms, box plots, and scatter plots. These visualizations will allow you to "see" the distribution of your data, making concepts like central tendency, dispersion, and outliers immediately obvious.