Menu
Stuff by Yuki
  • Home
  • Data Engineering
    • Python
  • Business Intelligence
    • Power BI
    • Tableau
  • Perspectives
  • About
  • Contact
Stuff by Yuki

Group Rows into List in Polars

Posted on June 2, 2023November 5, 2023

I recently encountered a situation where I wanted to consolidate or group rows per group value into a Python list. There seems to be various solutions in pandas (a few resources at the bottom), but how can you do this in Polars?

There are probably multiple ways you can do it in Polars as well. One way I found is using groupby(). It’s easy and simple to implement.

The code I use for this post is found here: GitHub repo

How to Do it

Let’s say you have a dataset like this. Each letter has multiple values. And we want to these values per letter.

Copy Copied Use a different Browser

import polars as pl

lf = pl.LazyFrame(
    {
        'Letter': ['A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'D'],
        'Value': [1, 2, 3, 4, 5, 6, 7, 8, 9]
    }
)

All you have to do is to:

  • Use groupby() to group by letters
  • Use agg() to group multiple values or rows into a Python list per letter
Copy Copied Use a different Browser

lf = (
    lf
    .groupby('Letter')
    .agg(pl.col('Value'))
    .sort('Letter')
)

The whole code looks like this:

Copy Copied Use a different Browser

import polars as pl

lf = pl.LazyFrame(
    {
        'Letter': ['A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'D'],
        'Value': [1, 2, 3, 4, 5, 6, 7, 8, 9]
    }
)

lf = (
    lf
    .groupby('Letter')
    .agg(pl.col('Value'))
    .sort('Letter')
)

print(lf.fetch())
'''
output:
shape: (4, 2)
┌────────┬───────────┐
│ Letter ┆ Value     │
│ ---    ┆ ---       │
│ str    ┆ list[i64] │
╞════════╪═══════════╡
│ A      ┆ [1, 2, 3] │
│ B      ┆ [4, 5, 6] │
│ C      ┆ [7, 8]    │
│ D      ┆ [9]       │
└────────┴───────────┘
'''

References

  • https://stackoverflow.com/questions/22219004/how-to-group-dataframe-rows-into-list-in-pandas-groupby
  • https://sparkbyexamples.com/pandas/pandas-group-dataframe-rows-list-groupby/

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Where I’m Headed in the Next 5 Years
  • Open-Source vs Vendor Data Tools
  • Developing the Habit of Writing
  • How to Inspect and Optimize Query Plans in Python Polars
  • Learn Python Polars with Polars Cookbook

Popular Posts

  • A Running Total Calculation with Quick Measure in Power BI
  • A Complete Guide to Git Integration in Power BI
  • How To Copy And Paste Report Page in Power BI
  • Handling Missing Values in Polars
  • How to Convert String to Date or Datetime in Polars

connect with me

  • LinkedIn
  • Twitter
  • Github
  • Website

Search Articles

©2025 Stuff by Yuki | Powered by SuperbThemes