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

.pivot should offer a column prefix option. #16663

Open
bfeif opened this issue Jun 2, 2024 · 0 comments
Open

.pivot should offer a column prefix option. #16663

bfeif opened this issue Jun 2, 2024 · 0 comments
Labels
A-ops Area: operations enhancement New feature or an improvement of an existing feature

Comments

@bfeif
Copy link
Contributor

bfeif commented Jun 2, 2024

Description

For readability of the results of pivot, users should be able to add a prefix to the columns that are produced. Some similar functionality appears to be included in the separator argument, but this is only used for repeating values, and doesn't allow for prefixing with something other than the columns argument itself.

Desired behavior:

import polars as pl

orders_df = pl.DataFrame({
    "customer": ["dan", "stan", "dan", "stan", "stan"],
    "basket_size": [1, 2, 3, 2, 1]
})

pivot = (
    orders_df
    .pivot(
        columns="basket_size",
        index="customer",
        values="customer",
        aggregate_function="len",
        column_prefix="num_orders_basket_size_eq_"
    )
)

print(pivot)
shape: (2, 4)
┌──────────┬───────────────────────────────┬────────────────────────────────┬────────────────────────────────┐
│ customer ┆ num_orders_basket_size_eq_1   ┆ num_orders_basket_size_eq_2    ┆ num_orders_basket_size_eq_3    │
│ ---      ┆ ---                           ┆ ---                            ┆ ---                            │
│ str      ┆ u32                           ┆ u32                            ┆ u32                            │
╞══════════╪═══════════════════════════════╪════════════════════════════════╪════════════════════════════════╡
│ dan      ┆ 1                             ┆ null                           ┆ 1                              │
│ stan     ┆ 1                             ┆ 2                              ┆ null                           │
└──────────┴───────────────────────────────┴────────────────────────────────┴────────────────────────────────┘

Possibly Related Issues

#14491
#15775

@bfeif bfeif added the enhancement New feature or an improvement of an existing feature label Jun 2, 2024
@MarcoGorelli MarcoGorelli added the A-ops Area: operations label Jun 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ops Area: operations enhancement New feature or an improvement of an existing feature
Projects
None yet
Development

No branches or pull requests

2 participants