Python para Data Science e Analytics
Python para Data Science e Analytics › Guides › Descriptive Statistics in Python: How to Calculate and Inter

Descriptive Statistics in Python: How to Calculate and Interpret

Descriptive statistics in Python is the set of calculations that summarizes a dataset before any model: central tendency (mean, median, mode), spread (standard deviation, variance, interquartile range), and position (quartiles and percentiles). With NumPy and Pandas, these measures come out in a few lines of code. The part that takes longer is interpreting what the numbers say about the data — and that is where most first analyses go wrong.

Topics: estatistica, python, analise-de-dados

What does descriptive statistics answer before you model anything?

Before training any model, you need to know what you are working with: how many rows and columns there are, which values are missing, what range each variable covers, and whether any records are absurd. Descriptive statistics is that first summary, computed directly on the table, with no statistical model involved.

It answers three practical questions: where the data concentrates (central tendency), how much it varies (spread), and how it sits along the scale (position and shape). These are direct calculations — no hypothesis, no sampling.

In Pandas the usual starting point is df.describe(), which returns count, mean, standard deviation, minimum, quartiles, and maximum for the numeric columns. It is a quick snapshot, not the whole analysis: it does not show missing values per column or the type of each variable, so pair it with df.info() and df.isna().sum().

How do you calculate mean, median, and mode with Pandas?

The mean adds every value and divides by the count, so it is sensitive to extremes. On a single column, df['column'].mean() returns one number; applied to the DataFrame, df.mean(numeric_only=True) returns one mean per numeric column.

The median is the middle value of the sorted series. df['column'].median() is not dragged by extreme values, which is why it is often the preferred measure for income, price, salary, and waiting time. The mode is the most frequent value: df['column'].mode() returns a Series, because two or more values can tie.

For categorical variables, mean and median make no sense — the summary is frequency. df['category'].value_counts(normalize=True) gives the share of each category, which is the correct descriptive reading for that kind of column.

If you work with plain arrays, NumPy does the same arithmetic: np.mean(series) and np.median(series). The practical difference is that Pandas carries the index and labels along with the values, which makes it easier to check which rows a result refers to.

How do you measure spread: standard deviation, variance, and interquartile range?

Two datasets with the same mean can be completely different. Standard deviation, df['column'].std(), tells you how far values sit from the mean in the same unit as the variable — if the column is in dollars, the deviation is in dollars too. Variance, var(), is the standard deviation squared and is more useful inside calculations than for direct reading.

Quartiles split the sorted data into four parts and are requested like this: df['column'].quantile([0.25, 0.5, 0.75]). The interquartile range (IQR) is the distance between the third and the first quartile, and it holds the central 50% of the sample.

The range, max - min, is simple to compute and easy to misread: one extreme value inflates it. The coefficient of variation — standard deviation divided by the mean — only makes sense for positive variables; in exchange, it lets you compare columns on different scales, such as weight in kilograms and delivery time in minutes.

Why do outliers change how you read the mean?

If a salary column has mean 5,000 and median 3,200, the gap between the two is a signal: high values are pulling the mean up. The median usually keeps representing the typical case; the mean does not. Comparing both before deciding which one to report is a habit that prevents many wrong conclusions.

The classic IQR rule flags a value as suspicious when it falls below Q1 - 1.5 × IQR or above Q3 + 1.5 × IQR. The rule is a screening device, not a verdict: an unusual record can be a data-entry error or the most informative row in the table. Telling them apart means looking at the record, not just at the number.

Visual checks help at this point. A histogram shows whether the distribution is symmetric or skewed, and a box plot puts the quartiles and the flagged points side by side. Both are single calls in the plotting libraries commonly used with Pandas. Keeping or removing a flagged record changes every measure computed afterwards, so write the decision down.

How do you run a first descriptive pass in Pandas?

A workable order: load the data, run df.info() and df.isna().sum(), call describe() on the numeric columns, then compare mean and median per column and check value_counts() for the categorical ones. Move to histograms and box plots only after that.

Write the numbers down. A short sheet with count, missing share, mean, median, standard deviation, and IQR per variable makes the next decisions traceable: which columns need cleaning, which need a transformation, and which add nothing to the analysis.

This is also the point where the results stop being mechanical. A standard deviation larger than the mean, a median far from the mean, or 40% missing in a column that looked central all change what you can conclude from the rest of the table.

Where do NumPy, Pandas, and these measures fit in the course?

The Python for Data Science and Analytics course is built in three main modules — Basic, Intermediate, and Advanced — plus bonus modules, and it covers the tools used on this page: IDE and Jupyter Notebook setup, basic functions, NumPy and Pandas, user-defined functions, loops and conditionals, data visualization, data cleaning and preparation, and outlier detection.

The later stage goes into machine learning: linear and logistic regression, K-Means, SVM, Naive Bayes, decision trees, Random Forest, Gradient Boosting, XGBoost, LightGBM, feature selection, and cross-validation. It closes with a final project that runs exploratory analysis and machine learning end to end on the same dataset.

Lessons are recorded, so you watch at your own pace, and access stays open with no expiry date. There are guided exercises, real market projects for a portfolio, a completion certificate, and an exclusive community for networking. No previous programming knowledge is required to start.

Price is R$ 470, or 12 installments of R$ 20.37 on a credit card. Payment runs through the Hotmart checkout, with card or boleto. Course details are on python.paanalytics.net.

Start at the basics and finish with an end-to-end machine learning project

Secure payment via Hotmart, up to 12 installments.

Enroll now Product site

Frequently asked questions

Do I need a statistics background before computing these measures in Python?

No. The calculations on this page are arithmetic: a sum divided by a count, a middle value, a subtraction. What takes practice is judging which measure answers your question. The course assumes no previous programming experience and begins with IDE and Jupyter Notebook setup.

Should I report the mean or the median?

Report both when they differ, and lead with the one that matches the question. The mean uses every value, which is useful for totals and downstream calculations; the median describes the typical case when the distribution is skewed or has extreme records.

Is df.describe() enough for a first analysis?

No. It skips non-numeric columns entirely, does not show how much is missing per column, and does not reveal data types. Treat it as the first snapshot and follow with df.info(), df.isna().sum(), and value_counts() on the categorical columns.

How should I handle missing values before computing these measures?

Pandas ignores missing values in mean, median, and standard deviation by default, and the count in describe() reflects only the non-null entries. Decide whether to drop or fill those rows, and state the choice next to the numbers, because the two options give different summaries.

Does the course cover NumPy and Pandas, and how long do I keep access?

Yes — NumPy, Pandas, data cleaning, preparation, and outlier detection are part of the curriculum, alongside visualization and machine learning. Access is unlimited with no expiry date, and the course issues a completion certificate.

Ler em português →