Q&A library Python

How do I quickly understand an unfamiliar DataFrame in pandas?

Python Beginner describeinfoprofiling

Short answer

Run info for the shape, dtypes and missing counts, describe for the numeric distribution, and nunique to spot identifiers and constants. These three checks catch most data problems before you start analysing.

The short version

Start with info() for the shape, column types and non-missing counts, describe() for the numeric distribution, and nunique() to spot identifier and constant columns. Doing these three checks first catches most type and quality problems before they quietly corrupt your analysis.

The first three commands

import pandas as pd

df.info()        # rows, columns, dtypes, non-null counts, memory
df.describe()    # count, mean, std, min, quartiles, max for numeric columns
df.nunique()     # how many distinct values in each column

Together these tell you how big the data is, what types it thinks it has, where values are missing, how the numbers are distributed, and which columns are identifiers or constants.

What info tells you that matters

The dtype column is the important part. A column of numbers showing as object means it is stored as text, usually because of a stray character or a thousands separator, and arithmetic on it will fail or behave oddly. A date column showing as object means it was never parsed and will sort alphabetically. Spotting these here saves hours later.

The non-null count against the row count shows missing data at a glance. A column with far fewer non-null values than rows is mostly empty, which may make it useless.

What describe reveals

Look at min and max for impossible values (a negative quantity, an age of 500), and compare mean with 50% (the median). A mean far above the median means a right-skewed distribution, usually a few large outliers, which tells you the mean is a poor summary and you should probably use the median.

By default describe covers numeric columns only. Pass include="all" to also summarise text columns, giving their most frequent value and how often it appears.

df.describe(include="all")

What nunique reveals

A column whose distinct count equals the row count is probably an identifier, useful for joining but meaningless to average. A column with a single distinct value is constant and carries no information. A text column with few distinct values is a category, and a good candidate for the category dtype to save memory.

Look at actual rows too

Summaries hide the texture of the data, so always look at some real rows.

df.head()          # first rows
df.sample(5)       # a random five, less biased than the first five

sample is worth preferring, because the first rows of a file are often unrepresentative (sorted, or full of early test records).

Check missingness explicitly

df.isna().sum()            # missing count per column
df.isna().mean().round(2)  # missing proportion per column

The proportion is often more useful than the count, because it tells you immediately whether a column is 2 per cent missing (fillable) or 90 per cent missing (probably unusable).

A short routine

Run info, describe(include="all"), nunique, isna().mean() and sample(5) on any new dataset before doing anything else. It takes seconds and reliably surfaces the type errors, missing data and odd distributions that would otherwise ambush you three steps into the analysis.

Enjoying these?

Go from piecing together answers to real fluency.

Our courses take you from looking things up to doing the work with confidence, step by step on real data.

Browse the courses