Pandas Analyzing Data

Before cleaning or transforming a DataFrame, use head(), tail(), info(), and describe() to quickly understand its shape, types, and distribution.

First Look: head() and tail()

Real-world datasets can have thousands or millions of rows, so printing the whole DataFrame is rarely useful. pandas gives you head() and tail() to preview the beginning and end of a DataFrame so you can confirm it loaded correctly and get a feel for what the columns contain before you do anything else.

Example

import pandas as pd

data = {
    'WorkoutMinutes': [30, 45, 60, 60, 45, 60],
    'HeartRate': [109, 117, 110, 104, 117, 98],
    'MaxHeartRate': [175, 148, 130, 132, 148, 124],
    'CaloriesBurned': [409.1, 479.0, 340.0, 320.8, 320.0, 300.0]
}
df = pd.DataFrame(data)

print(df.head(3))   # first 3 rows
print(df.tail(2))   # last 2 rows
  • head(n) returns the first n rows and defaults to 5 if you omit n
  • tail(n) returns the last n rows and also defaults to 5
  • Both return a new DataFrame, so you can chain methods after them, e.g. df.head().describe()
  • A quick head()/tail() check will reveal shifted columns, wrong headers, or an unexpected sort order

Digging Deeper with info()

head() and tail() show you values, but info() shows you structure. It reports the number of rows, the column names in order, how many non-null values each column has, and the data type pandas inferred for each column — all in a single call.

Example

import pandas as pd
import numpy as np

data = {
    'WorkoutMinutes': [30, 45, 60, 60, 45, 60],
    'HeartRate': [109, 117, 110, 104, 117, 98],
    'MaxHeartRate': [175, 148, 130, 132, 148, 124],
    'CaloriesBurned': [409.1, 479.0, np.nan, 320.8, 320.0, 300.0]
}
df = pd.DataFrame(data)

df.info()
# RangeIndex: 6 entries, 0 to 5
# Data columns (total 4 columns):
#  #   Column          Non-Null Count  Dtype
# ---  ------          --------------  -----
#  0   WorkoutMinutes  6 non-null      int64
#  1   HeartRate       6 non-null      int64
#  2   MaxHeartRate    6 non-null      int64
#  3   CaloriesBurned  5 non-null      float64
Note: Compare the Non-Null Count across columns. CaloriesBurned shows 5 non-null out of 6 rows, which means it contains one empty (NaN) cell you will likely need to handle before analysis.

Summary Statistics with describe()

describe() computes summary statistics for every numeric column in one call: count, mean, standard deviation, minimum, maximum, and the 25th, 50th, and 75th percentiles. It is the fastest way to sanity-check whether your numbers look reasonable before you build charts or models on top of them.

Example

import pandas as pd

data = {
    'WorkoutMinutes': [30, 45, 60, 60, 45, 60],
    'HeartRate': [109, 117, 110, 104, 117, 98],
    'MaxHeartRate': [175, 148, 130, 132, 148, 124],
    'CaloriesBurned': [409.1, 479.0, 340.0, 320.8, 320.0, 300.0]
}
df = pd.DataFrame(data)

print(df.describe())
# Prints count, mean, std, min, 25%, 50%, 75%, and max
# for every numeric column, e.g. df.describe()['CaloriesBurned']['mean']
StatisticWhat it tells you
countHow many non-empty values are in the column
meanThe average value across all rows
stdHow spread out the values are around the mean
min / maxThe smallest and largest values in the column
25% / 50% / 75%The value below which that percentage of rows fall (the 50% row is the median)
Note: describe() only summarizes numeric columns by default. To include text columns as well, call df.describe(include='all'); pandas will add unique, top, and freq rows describing the most common categorical values.

Exercise: Pandas Analyzing Data

By default, how many rows does head() display if no argument is given?