Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

Polars: create an integer column from another one by computing the difference with the largest smaller value from a list

$
0
0

I have a polars dataframe like this one:

shape: (10, 2)┌────────┬───────┐│ foo    ┆ bar   ││ ---    ┆ ---   ││ i64    ┆ i64   │╞════════╪═══════╡│ 86     ┆ 11592 ││ 109    ┆ 2765  ││ 109    ┆ 4228  ││ 153    ┆ 4214  ││ 153    ┆ 7217  ││ 153    ┆ 11095 ││ 160    ┆ 1134  ││ 222    ┆ 5509  ││ 225    ┆ 10150 ││ 239    ┆ 4151  │└────────┴───────┘

And a sorted list of integers points:

points = [0, 1500, 3000, 4500, 6000, 7500, 9000, 10500, 12000]

I want to create a new column baz, such that for each element y of bar, I find the largest x in points such that x =< y. Then the element of baz is y - x. How can I do that?


Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>