🎓 A1Learner
🔍
0 of 50 lessons completed

Python ML Explore Your Data First

Exploring data means looking at its shape and spread before any model. A quick look catches surprises that would spoil later steps.

The Iris table at a glance

The table holds 150 rows and 5 columns. The last column names the species.

import pandas as pd
from sklearn.datasets import load_iris
iris = load_iris()
df = pd.DataFrame(iris.data, columns=iris.feature_names)
df["species"] = iris.target_names[iris.target]
print(df.shape)
print(list(df.columns))

One mean from describe()

describe() works out the mean of each number column. The mean sepal length is 5.84, and the longest is 7.9.

import pandas as pd
from sklearn.datasets import load_iris
iris = load_iris()
df = pd.DataFrame(iris.data, columns=iris.feature_names)
df["species"] = iris.target_names[iris.target]
print(round(float(df.describe().loc["mean", "sepal length (cm)"]), 2))
print(round(float(df.describe().loc["max", "sepal length (cm)"]), 1))

Three species, fifty flowers each

value_counts() counts each species. The three groups are exactly even.

import pandas as pd
from sklearn.datasets import load_iris
iris = load_iris()
df = pd.DataFrame(iris.data, columns=iris.feature_names)
df["species"] = iris.target_names[iris.target]
print(df["species"].value_counts().to_dict())

No gaps in the Iris table

isna().sum().sum() counts every missing value. The Iris table has none.

import pandas as pd
from sklearn.datasets import load_iris
iris = load_iris()
df = pd.DataFrame(iris.data, columns=iris.feature_names)
df["species"] = iris.target_names[iris.target]
print(int(df.isna().sum().sum()))

Example

import pandas as pd
from sklearn.datasets import load_iris
iris = load_iris()
df = pd.DataFrame(iris.data, columns=iris.feature_names)
df["species"] = iris.target_names[iris.target]
print("Rows:", len(df))
print("Species counts:", df["species"].value_counts().to_dict())
print("Petal length mean:", round(float(df["petal length (cm)"].mean()), 2))
print("Petal length smallest:", float(df["petal length (cm)"].min()))
print("Petal length largest:", float(df["petal length (cm)"].max()))
print("Missing values:", int(df.isna().sum().sum()))

Key points

  • describe(): a DataFrame method that prints count, mean, and spread for each number column
  • value_counts(): a method that counts how often each value shows up
  • Distribution: how the values spread out, clumped together or spread wide
  • Use df.shape first. It tells you how many rows and columns you hold.
  • Use value_counts() on a label column to check the groups are balanced
  • Use isna().sum().sum() to count every missing value in the whole table

In this example, the Iris data comes built into scikit-learn, so no file is needed. The table holds 150 flowers in three even groups of 50. The mean petal length is 3.76, from 1.0 up to 6.9. No value is missing. Looking first takes a few lines, and it proves the data is clean before any later step uses it.

Note

Explore before you change anything. A fix made blind can hide the real problem.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Python Playground

Exercise

What does value_counts() do?