Skip to content

Commit

Permalink
docs(python): Clarify examples for .transpose() (pola-rs#13581)
Browse files Browse the repository at this point in the history
  • Loading branch information
wjandrea authored Jan 10, 2024
1 parent 10ef186 commit 0f8992c
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions py-polars/polars/dataframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3926,7 +3926,7 @@ def transpose(
Examples
--------
>>> df = pl.DataFrame({"a": [1, 2, 3], "b": [1, 2, 3]})
>>> df = pl.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
>>> df.transpose(include_header=True)
shape: (2, 4)
┌────────┬──────────┬──────────┬──────────┐
Expand All @@ -3935,35 +3935,35 @@ def transpose(
│ str ┆ i64 ┆ i64 ┆ i64 │
╞════════╪══════════╪══════════╪══════════╡
│ a ┆ 1 ┆ 2 ┆ 3 │
│ b ┆ 123
│ b ┆ 456
└────────┴──────────┴──────────┴──────────┘
Replace the auto-generated column names with a list
>>> df.transpose(include_header=False, column_names=["a", "b", "c"])
>>> df.transpose(include_header=False, column_names=["x", "y", "z"])
shape: (2, 3)
┌─────┬─────┬─────┐
abc
xyz
│ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ i64 │
╞═════╪═════╪═════╡
│ 1 ┆ 2 ┆ 3 │
123
456
└─────┴─────┴─────┘
Include the header as a separate column
>>> df.transpose(
... include_header=True, header_name="foo", column_names=["a", "b", "c"]
... include_header=True, header_name="foo", column_names=["x", "y", "z"]
... )
shape: (2, 4)
┌─────┬─────┬─────┬─────┐
│ foo ┆ abc
│ foo ┆ xyz
│ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ i64 ┆ i64 ┆ i64 │
╞═════╪═════╪═════╪═════╡
│ a ┆ 1 ┆ 2 ┆ 3 │
│ b ┆ 123
│ b ┆ 456
└─────┴─────┴─────┴─────┘
Replace the auto-generated column with column names from a generator function
Expand All @@ -3982,31 +3982,31 @@ def transpose(
│ i64 ┆ i64 ┆ i64 │
╞═════════════╪═════════════╪═════════════╡
│ 1 ┆ 2 ┆ 3 │
123
456
└─────────────┴─────────────┴─────────────┘
Use an existing column as the new column names
>>> df = pl.DataFrame(dict(id=["a", "b", "c"], col1=[1, 3, 2], col2=[3, 4, 6]))
>>> df = pl.DataFrame(dict(id=["i", "j", "k"], a=[1, 2, 3], b=[4, 5, 6]))
>>> df.transpose(column_names="id")
shape: (2, 3)
┌─────┬─────┬─────┐
abc
ijk
│ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ i64 │
╞═════╪═════╪═════╡
│ 1 ┆ 32
34 ┆ 6 │
│ 1 ┆ 23
45 ┆ 6 │
└─────┴─────┴─────┘
>>> df.transpose(include_header=True, header_name="new_id", column_names="id")
shape: (2, 4)
┌────────┬─────┬─────┬─────┐
│ new_id ┆ abc
│ new_id ┆ ijk
│ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ i64 ┆ i64 ┆ i64 │
╞════════╪═════╪═════╪═════╡
col1 ┆ 1 ┆ 32
col2 ┆ 34 ┆ 6 │
a ┆ 1 ┆ 23
b ┆ 45 ┆ 6 │
└────────┴─────┴─────┴─────┘
"""
keep_names_as = header_name if include_header else None
Expand Down

0 comments on commit 0f8992c

Please sign in to comment.