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

Nested polars.col()

I would like to get value from column whose name is in another column.

In my table there are many columns, so doing this with pl.when().then() is not an option.

As input we have this dataframe:

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

foo foo_count bar bar_count baz baz_count largest
1 23 4 43 5 64 baz
2 45 6 45 1 43 bar
3 234 9 453 15 231 baz
4 55 2 67 3 94 foo

and would like transform this with with_columns():

foo foo_count bar bar_count baz baz_count largest largest_count
1 23 4 43 5 64 baz 64
2 45 6 45 1 43 bar 45
3 234 9 453 15 231 baz 231
4 55 2 67 3 94 foo 4

This (pseudo)code illustrates what I have in mind (but ofc. this doesn’t work)

df = pl.DataFrame({"foo" : [1, 2, 3, 4],
                   "foo_count" : [23, 45 ,234, 55],
                   "bar" : [4 ,6 ,9, 2],
                   "bar_count" : [43, 45 ,453, 67],
                   "baz": [5,1,15, 3],
                   "baz_count" : [64, 43 ,231, 94],
                   "largest" : ["baz", "bar", "baz", "foo"]})

df.with_columns(
    pl.col(f"{pl.col('largest')}_count").alias("largest_count")
)

>Solution :

Here is a simple approach. First, we create expressions for masked columns using a when-then expression. Then, we rely on pl.coalesce to combine the masked columns. Still there is a naive python loop over the unique elements in largest.

df.with_columns(
    pl.coalesce(
        pl.when(pl.col("largest") == col).then(pl.col(f"{col}_count"))
        for col in df.get_column("largest").unique()
    ).alias("largest_count")
)
shape: (4, 8)
┌─────┬───────────┬─────┬───────────┬─────┬───────────┬─────────┬───────────────┐
│ foo ┆ foo_count ┆ bar ┆ bar_count ┆ baz ┆ baz_count ┆ largest ┆ largest_count │
│ --- ┆ ---       ┆ --- ┆ ---       ┆ --- ┆ ---       ┆ ---     ┆ ---           │
│ i64 ┆ i64       ┆ i64 ┆ i64       ┆ i64 ┆ i64       ┆ str     ┆ i64           │
╞═════╪═══════════╪═════╪═══════════╪═════╪═══════════╪═════════╪═══════════════╡
│ 1   ┆ 23        ┆ 4   ┆ 43        ┆ 5   ┆ 64        ┆ baz     ┆ 64            │
│ 2   ┆ 45        ┆ 6   ┆ 45        ┆ 1   ┆ 43        ┆ bar     ┆ 45            │
│ 3   ┆ 234       ┆ 9   ┆ 453       ┆ 15  ┆ 231       ┆ baz     ┆ 231           │
│ 4   ┆ 55        ┆ 2   ┆ 67        ┆ 3   ┆ 94        ┆ foo     ┆ 55            │
└─────┴───────────┴─────┴───────────┴─────┴───────────┴─────────┴───────────────┘
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