Pandas Analyzing Data
When you receive a new dataset, the first job is to get to know it: how big is it, what columns does it have, what types, how many values are missing, and what do the numbers roughly look like? pandas has a handful of methods made exactly for this first look.
We will use a small table of trips from a bike-sharing service throughout this lesson.
head() and tail()
import io
import pandas as pd
csv_text = """trip_id,station,minutes,distance_km,member
101,Central,12,2.8,yes
102,Riverside,35,7.9,no
103,Central,8,1.6,yes
104,Park,,3.1,yes
105,Riverside,22,5.4,yes
106,Park,51,,no
107,Central,15,3.3,no
108,Park,9,2.0,yes
"""
trips = pd.read_csv(io.StringIO(csv_text))
print(trips.head())
print(trips.tail(2)) trip_id station minutes distance_km member
0 101 Central 12.0 2.8 yes
1 102 Riverside 35.0 7.9 no
2 103 Central 8.0 1.6 yes
3 104 Park NaN 3.1 yes
4 105 Riverside 22.0 5.4 yes
trip_id station minutes distance_km member
6 107 Central 15.0 3.3 no
7 108 Park 9.0 2.0 yes
head() shows the first 5 rows by default and tail() the last 5; pass a number for a different count. Already you can spot an empty cell (NaN) in the minutes column, something to deal with later.
info()
info() prints a compact summary: the index range, every column with its count of non-missing values and its dtype, and the memory used.
import io
import pandas as pd
csv_text = """trip_id,station,minutes,distance_km,member
101,Central,12,2.8,yes
102,Riverside,35,7.9,no
103,Central,8,1.6,yes
104,Park,,3.1,yes
105,Riverside,22,5.4,yes
106,Park,51,,no
107,Central,15,3.3,no
108,Park,9,2.0,yes
"""
trips = pd.read_csv(io.StringIO(csv_text))
trips.info()<class 'pandas.core.frame.DataFrame'>
RangeIndex: 8 entries, 0 to 7
Data columns (total 5 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 trip_id 8 non-null int64
1 station 8 non-null object
2 minutes 7 non-null float64
3 distance_km 7 non-null float64
4 member 8 non-null object
dtypes: float64(2), int64(1), object(2)
memory usage: 328.0+ bytes
Reading the output:
- RangeIndex: 8 entries means 8 rows.
minutesanddistance_kmhave 7 non-null values, so each is missing one value.minutesisfloat64even though the data are whole numbers: a column with NaN cannot be a plain integer column.
Note:
info()prints directly and returnsNone, so call it on its own line instead of insideprint().
describe()
describe() computes summary statistics for every numeric column:
import io
import pandas as pd
csv_text = """trip_id,station,minutes,distance_km,member
101,Central,12,2.8,yes
102,Riverside,35,7.9,no
103,Central,8,1.6,yes
104,Park,,3.1,yes
105,Riverside,22,5.4,yes
106,Park,51,,no
107,Central,15,3.3,no
108,Park,9,2.0,yes
"""
trips = pd.read_csv(io.StringIO(csv_text))
print(trips[["minutes", "distance_km"]].describe().round(2))
print(trips.describe(include="object")) minutes distance_km
count 7.00 7.00
mean 21.71 3.73
std 15.93 2.20
min 8.00 1.60
25% 10.50 2.40
50% 15.00 3.10
75% 28.50 4.35
max 51.00 7.90
station member
count 8 8
unique 3 2
top Central yes
freq 3 5
| Row | Meaning |
|---|---|
count |
non-missing values |
mean, std |
average and standard deviation |
min, max |
smallest and largest value |
25%, 50%, 75% |
quartiles (50% is the median) |
For text columns (include="object") you get count, the number of unique values, the most frequent value (top) and how often it appears (freq).
Other quick checks
import io
import pandas as pd
csv_text = """trip_id,station,minutes,distance_km,member
101,Central,12,2.8,yes
102,Riverside,35,7.9,no
103,Central,8,1.6,yes
104,Park,,3.1,yes
105,Riverside,22,5.4,yes
106,Park,51,,no
107,Central,15,3.3,no
108,Park,9,2.0,yes
"""
trips = pd.read_csv(io.StringIO(csv_text))
print(trips.shape)
print(trips.isna().sum())
print(trips["station"].value_counts())
print(trips["minutes"].max(), trips["minutes"].idxmax())(8, 5)
trip_id 0
station 0
minutes 1
distance_km 1
member 0
dtype: int64
station
Central 3
Park 3
Riverside 2
Name: count, dtype: int64
51.0 5
| Method | Answers the question |
|---|---|
df.shape |
How many rows and columns? |
df.isna().sum() |
How many values are missing in each column? |
df["col"].value_counts() |
How often does each value occur? |
df["col"].nunique() |
How many distinct values? |
df["col"].idxmax() |
Which row holds the largest value? |
df.sample(n, random_state=0) |
A few random rows (reproducibly) |
These checks take seconds and often reveal problems (missing values, wrong types, impossible numbers) that the next section, Cleaning Data, will fix.
Test yourself
How many rows does df.head() show by default?
- 3
- 5
- 10
- All of them
Which method reports the number of non-null values and the dtype of every column?
- describe()
- info()
- shape
- value_counts()
In describe() output, which row is the median?
- mean
- 25%
- 50%
- std