
Polars is a fast and efficient data manipulation library for Python, offering a powerful alternative to pandas. The describe() method in Polars provides a quick way to compute basic descriptive statistics for a DataFrame, giving you a comprehensive overview of your data at a glance.
Introduction to the Iris Dataset
Before we dive into the describe() method, let’s introduce the dataset we’ll be working with: the Iris dataset. This famous dataset, introduced by statistician Ronald Fisher in 1936, is often used for introductory data science and machine learning tasks.
The Iris dataset contains 150 samples from three species of Iris flowers: Iris setosa, Iris virginica, and Iris versicolor. For each sample, four features are measured:
- Sepal length (in cm)
- Sepal width (in cm)
- Petal length (in cm)
- Petal width (in cm)
This dataset is particularly useful for demonstrating descriptive statistics because it contains multiple numeric features that exhibit different distributions and relationships. By analyzing these features, we can gain insights into the characteristics of different Iris species and how they might be distinguished from one another.
Understanding the describe() Method
The describe() method in Polars calculates summary statistics for numerical columns in a DataFrame. By default, it computes the count, null count, mean, standard deviation, minimum, 25th percentile, median, 75th percentile, and maximum for each numeric column.
Let’s explore how to use describe() with some practical examples using the Iris dataset.
Example 1: Basic Usage of describe() with Iris Dataset
First, let’s load the Iris dataset and use the describe() method to get an overview of its statistics.
import polars as pl from sklearn.datasets import load_iris # Load the Iris dataset iris = load_iris() df = pl.DataFrame(iris.data, schema=iris.feature_names) # Compute basic statistics stats = df.describe() print(stats)
This will output:
shape: (9, 5) ┌────────────┬───────────────────┬──────────────────┬───────────────────┬──────────────────┐ │ statistic ┆ sepal length (cm) ┆ sepal width (cm) ┆ petal length (cm) ┆ petal width (cm) │ │ --- ┆ --- ┆ --- ┆ --- ┆ --- │ │ str ┆ f64 ┆ f64 ┆ f64 ┆ f64 │ ╞════════════╪═══════════════════╪══════════════════╪═══════════════════╪══════════════════╡ │ count ┆ 150.0 ┆ 150.0 ┆ 150.0 ┆ 150.0 │ │ null_count ┆ 0.0 ┆ 0.0 ┆ 0.0 ┆ 0.0 │ │ mean ┆ 5.843333 ┆ 3.057333 ┆ 3.758 ┆ 1.199333 │ │ std ┆ 0.828066 ┆ 0.435866 ┆ 1.765298 ┆ 0.762238 │ │ min ┆ 4.3 ┆ 2.0 ┆ 1.0 ┆ 0.1 │ │ 25% ┆ 5.1 ┆ 2.8 ┆ 1.6 ┆ 0.3 │ │ 50% ┆ 5.8 ┆ 3.0 ┆ 4.4 ┆ 1.3 │ │ 75% ┆ 6.4 ┆ 3.3 ┆ 5.1 ┆ 1.8 │ │ max ┆ 7.9 ┆ 4.4 ┆ 6.9 ┆ 2.5 │ └────────────┴───────────────────┴──────────────────┴───────────────────┴──────────────────┘
This output provides a comprehensive summary of all features in the Iris dataset. We can see that there are 150 samples for each feature, with no null values. The mean and standard deviation give us an idea of the central tendency and spread of each feature, while the percentiles provide insights into the distribution.
Example 2: Customizing describe() Output
You can customize the statistics computed by describe() using the percentiles parameter. Let’s focus on custom percentiles for all features:
import polars as pl from sklearn.datasets import load_iris # Load the Iris dataset iris = load_iris() df = pl.DataFrame(iris.data, schema=iris.feature_names) # Compute custom percentiles custom_stats = df.describe(percentiles=[0.1, 0.5, 0.9]) print(custom_stats)
This will output:
shape: (9, 5) ┌────────────┬───────────────────┬──────────────────┬───────────────────┬──────────────────┐ │ statistic ┆ sepal length (cm) ┆ sepal width (cm) ┆ petal length (cm) ┆ petal width (cm) │ │ --- ┆ --- ┆ --- ┆ --- ┆ --- │ │ str ┆ f64 ┆ f64 ┆ f64 ┆ f64 │ ╞════════════╪═══════════════════╪══════════════════╪═══════════════════╪══════════════════╡ │ count ┆ 150.0 ┆ 150.0 ┆ 150.0 ┆ 150.0 │ │ null_count ┆ 0.0 ┆ 0.0 ┆ 0.0 ┆ 0.0 │ │ mean ┆ 5.843333 ┆ 3.057333 ┆ 3.758 ┆ 1.199333 │ │ std ┆ 0.828066 ┆ 0.435866 ┆ 1.765298 ┆ 0.762238 │ │ min ┆ 4.3 ┆ 2.0 ┆ 1.0 ┆ 0.1 │ │ 10% ┆ 4.8 ┆ 2.5 ┆ 1.4 ┆ 0.2 │ │ 50% ┆ 5.8 ┆ 3.0 ┆ 4.4 ┆ 1.3 │ │ 90% ┆ 6.9 ┆ 3.6 ┆ 5.8 ┆ 2.2 │ │ max ┆ 7.9 ┆ 4.4 ┆ 6.9 ┆ 2.5 │ └────────────┴───────────────────┴──────────────────┴───────────────────┴──────────────────┘
This output now includes the 10th, 50th (median), and 90th percentiles for all features, providing a different perspective on the data distribution. For example, we can see that 90% of the samples have a sepal length of 6.9 cm or less.
Example 3: Describing Specific Columns
Let’s say we’re particularly interested in the length measurements (sepal length and petal length). We can use describe() on specific columns:
import polars as pl from sklearn.datasets import load_iris # Load the Iris dataset iris = load_iris() df = pl.DataFrame(iris.data, schema=iris.feature_names) # Describe only length columns length_stats = df.select(['sepal length (cm)', 'petal length (cm)']).describe() print(length_stats)
This will output:
shape: (9, 3) ┌────────────┬───────────────────┬───────────────────┐ │ statistic ┆ sepal length (cm) ┆ petal length (cm) │ │ --- ┆ --- ┆ --- │ │ str ┆ f64 ┆ f64 │ ╞════════════╪═══════════════════╪═══════════════════╡ │ count ┆ 150.0 ┆ 150.0 │ │ null_count ┆ 0.0 ┆ 0.0 │ │ mean ┆ 5.843333 ┆ 3.758 │ │ std ┆ 0.828066 ┆ 1.765298 │ │ min ┆ 4.3 ┆ 1.0 │ │ 25% ┆ 5.1 ┆ 1.6 │ │ 50% ┆ 5.8 ┆ 4.4 │ │ 75% ┆ 6.4 ┆ 5.1 │ │ max ┆ 7.9 ┆ 6.9 │ └────────────┴───────────────────┴───────────────────┘
This output focuses only on the sepal length and petal length columns. We can observe that petal length has a much wider range (1.0 to 6.9 cm) compared to sepal length (4.3 to 7.9 cm), which might be a useful distinguishing feature among Iris species.
Conclusion
The describe() method in Polars is a useful tool for quickly understanding the basic statistical properties of your data. Using it with real-world datasets like the Iris dataset demonstrates its practical application in data analysis tasks.
Remember these key points:
- describe() provides a comprehensive summary of numerical data, including count, mean, standard deviation, and various percentiles.
- You can customize the percentiles calculated to focus on specific parts of the data distribution.
- It’s possible to apply describe() to specific columns, allowing you to focus on particular features of interest.
- This method is an excellent first step in exploratory data analysis, giving you a quick overview of your dataset’s characteristics before diving into more complex analyses or modeling tasks.
With a solid grasp of the describe() method, you’ll quickly gain valuable insights into your datasets, paving the way for more in-depth analysis in areas like feature selection and understanding data distributions.
