pbPassingBI
/
Working with DataFrames beginner 5 min

Inspecting a DataFrame

head, info, describe and shape — the four commands to run every time.

What you'll be able to do
  • Inspect a new dataset systematically
  • Read the output of info()
  • Spot problems before analysing

The first four commands

df.head()        # first 5 rows
df.shape         # (rows, columns)
df.info()        # column names, non-null counts, dtypes
df.describe()    # summary statistics for numeric columns

Run all four on any dataset before doing anything else. Two minutes here saves an hour of confusion later.

Reading info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1000 entries, 0 to 999
Data columns (total 4 columns):
 #   Column      Non-Null Count  Dtype
---  ------      --------------  -----
 0   order_id    1000 non-null   int64
 1   order_date  1000 non-null   object
 2   revenue     982 non-null    float64
 3   region      1000 non-null   object

Two problems are visible here:

  • order_date is object, meaning text, not a date. Date arithmetic will fail and sorting will be alphabetical.
  • revenue has 982 non-null out of 1000 — 18 missing values that will quietly skew any mean.

dtypes

dtypeMeaning
int64Whole numbers
float64Decimals — also any integer column containing NaN
objectUsually text, sometimes mixed types
datetime64[ns]Proper dates
boolTrue/False
categoryRepeated labels stored efficiently

An integer column becoming float64 is the usual sign that a null crept in.

Looking closer

df.columns              # column names
df['region'].unique()   # distinct values
df['region'].nunique()  # how many distinct
df['region'].value_counts()   # frequency, most common first
df.isnull().sum()       # missing values per column
df.sample(5)            # random rows, not just the first ones

value_counts() on every categorical column is the fastest way to find inconsistent labels — TX, Tx and Texas all present in one field.

Key points
  • Run head, shape, info and describe on every new dataset
  • dtype object on a date column means it is text
  • value_counts() exposes inconsistent category labels
Check yourself