Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow element wise computation based on another column in list.eval(pl.element() ... ) #17496

Closed
lyngc opened this issue Jul 8, 2024 · 3 comments
Labels
enhancement New feature or an improvement of an existing feature

Comments

@lyngc
Copy link

lyngc commented Jul 8, 2024

Description

Would like to do computation on each element in a list based on some scalar stored in another column. Could not figure out if this is already possible.

Example:

import polars as pl

df = pl.DataFrame({
    "some_list": [[1, 2, 3], [4, 5, 6], [7, 8, 9]],
    "scalar": [0.1, 0.2, 0.3]
})

df.with_columns(some_scaled_list=pl.col("some_list").list.eval(pl.element() * 0.2)) # Works
df.with_columns(some_scaled_list=pl.col("some_list").list.eval(pl.element() * pl.col("scalar"))) # Throws an error
@lyngc lyngc added the enhancement New feature or an improvement of an existing feature label Jul 8, 2024
@deanm0000
Copy link
Collaborator

semi similar to this

@cmdlineluser
Copy link
Contributor

This can now be done outside of .list.eval #19162

df.with_columns(result = pl.col.some_list * pl.col.scalar)
# shape: (3, 3)
# ┌───────────┬────────┬─────────────────┐
# │ some_list ┆ scalar ┆ result          │
# │ ---       ┆ ---    ┆ ---             │
# │ list[i64] ┆ f64    ┆ list[f64]       │
# ╞═══════════╪════════╪═════════════════╡
# │ [1, 2, 3] ┆ 0.1    ┆ [0.1, 0.2, 0.3] │
# │ [4, 5, 6] ┆ 0.2    ┆ [0.8, 1.0, 1.2] │
# │ [7, 8, 9] ┆ 0.3    ┆ [2.1, 2.4, 2.7] │
# └───────────┴────────┴─────────────────┘

@lyngc
Copy link
Author

lyngc commented Oct 15, 2024

That's nice, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or an improvement of an existing feature
Projects
None yet
Development

No branches or pull requests

4 participants