Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to select the last non-null value from one column and also the value from another column on the same row in Polars?

Below is a non working example in which I retrieve the last available ‘Open’ but how do I get corresponding ‘Time’?

sel = self.data.select([pl.col('Time'),
    pl.col('Open').drop_nulls().last()])

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

For instance, you can use .filter() to select rows that do not contain null and then take last row

Here example:

df = pl.DataFrame({
    "a": [1,2,3,4,5],
    "b": ["cat", None, "owl", None, None]
})
┌─────┬──────┐
│ a   ┆ b    │
│ --- ┆ ---  │
│ i64 ┆ str  │
╞═════╪══════╡
│ 1   ┆ cat  │
│ 2   ┆ null │
│ 3   ┆ owl  │
│ 4   ┆ null │
│ 5   ┆ null │
└─────┴──────┘
df.filter(
    pl.col("b").is_not_null()
).select(pl.all().last())
┌─────┬─────┐
│ a   ┆ b   │
│ --- ┆ --- │
│ i64 ┆ str │
╞═════╪═════╡
│ 3   ┆ owl │
└─────┴─────┘
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading