Aggregating values across multiple columns
Although aggregation typically means aggregating values in a column, there are cases where you might want to aggregate values horizontally or across multiple columns. And there are methods available in Python Polars that allow you to do that.
In this recipe, we’ll cover multiple ways to aggregate values across multiple columns.
Getting ready
In this recipe, we’ll use a Pokémon dataset:
df = pl.read_csv('../data/pokemon.csv') df.head()
The preceding code will return the following output:
Figure 4.18 – The first five rows of the Pokémon dataset
How to do it...
Here are ways to aggregate values across columns:
- Calculate the sum of all the stats of each Pokémon:
( df .select('HP', 'Attack', 'Defense', 'Sp. Atk', 'Sp. Def', 'Speed') ...