← Data analysis with Python

Looking at a new table

head, shape, columns, dtypes and describe: your first five minutes with any data.

Start this lesson

Before analyzing, look. A few commands give a quick picture of any DataFrame. df.head() shows the first 5 rows (df.tail() the last). df.shape is (rows, columns). df.columns lists the column names. df.dtypes shows each column’s type: int64 and float64 are numbers, object usually means text. df.describe() summarizes every number column at once: count, mean, standard deviation, min, quartiles and max.

import io
import pandas as pd

csv_text = """name,group,math,reading,hours
Amara,A,88,92,6
Ben,A,72,80,3
Chen,B,95,85,8
Dalia,B,64,70,2
Emeka,A,79,88,5
Fatima,B,91,94,7
Gus,A,58,65,1
Hana,B,83,77,5
"""
scores = pd.read_csv(io.StringIO(csv_text))
print(scores.head(3))
print(scores.shape)
print(scores.dtypes)
print(scores.describe())

shape is (8, 5): 8 students, 5 columns. dtypes shows name and group as object (text) and the rest as int64. describe() covers only number columns.

shape has no parentheses: it is a property, not a method. Writing scores.shape() gives an error.

describe() returns a DataFrame, so you can read one cell from it: scores.describe().loc["mean", "math"].

If a column you expect to be numbers shows dtype object, some values are probably text (like "n/a" or "$4"). You will fix that in the cleaning unit.