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

docs(python): Add example for separator parameter in pivot #15935

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 27 additions & 4 deletions py-polars/polars/dataframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -7033,9 +7033,7 @@ def pivot(
Parameters
----------
values
Column values to aggregate. Can be multiple columns if the *columns*
arguments contains multiple columns as well. If None, all remaining columns
will be used.
Column values to aggregate. If None, all remaining columns will be used.
index
One or multiple keys to group by.
columns
Expand All @@ -7053,7 +7051,7 @@ def pivot(
sort_columns
Sort the transposed columns by name. Default is by order of discovery.
separator
Used as separator/delimiter in generated column names.
Used as separator/delimiter in generated column names in case of multiple value columns.

Returns
-------
Expand Down Expand Up @@ -7150,6 +7148,31 @@ def pivot(
│ a ┆ 0.998347 ┆ null │
│ b ┆ 0.964028 ┆ 0.999954 │
└──────┴──────────┴──────────┘

Using a custom `separator` in generated column names:

>>> df = pl.DataFrame({
... 'ix': [1, 1, 2, 2, 1, 2],
... 'col': ['a', 'a', 'a', 'a', 'b', 'b'],
... 'foo': [0, 1, 2, 2, 7, 1],
... 'bar': [0, 2, 0, 0, 9, 4]
... })
>>> df.pivot(
... index='ix',
... columns='col',
... values=['foo', 'bar'],
... aggregate_function='sum',
... separator='/'
... )
shape: (2, 5)
┌─────┬───────────┬───────────┬───────────┬───────────┐
│ ix ┆ foo/col/a ┆ foo/col/b ┆ bar/col/a ┆ bar/col/b │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 │
╞═════╪═══════════╪═══════════╪═══════════╪═══════════╡
│ 1 ┆ 1 ┆ 7 ┆ 2 ┆ 9 │
│ 2 ┆ 4 ┆ 1 ┆ 0 ┆ 4 │
└─────┴───────────┴───────────┴───────────┴───────────┘
""" # noqa: W505
index = _expand_selectors(self, index)
columns = _expand_selectors(self, columns)
Expand Down