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

first_column_as_header in transpose #8095

Closed
stevenlis opened this issue Apr 9, 2023 · 3 comments
Closed

first_column_as_header in transpose #8095

stevenlis opened this issue Apr 9, 2023 · 3 comments
Labels
enhancement New feature or an improvement of an existing feature

Comments

@stevenlis
Copy link

Problem description

The method transpose could be very handy when you have to review a list of variables of ids. I already find it's much easier to scroll vertically than horizontally.

import polars as pl

df = pl.DataFrame(
    {"id": ['a', 'b', 'c'],
     "col1": [1, 3, 2],
     "col2": [3, 4, 6]}
)

Right now, you can do the following but the first row is redundant.

df.transpose(
    include_header=True,
    header_name='id',
    column_names=df['id']
)

shape: (3, 4)
┌──────┬─────┬─────┬─────┐
│ idabc   │
│ ------------ │
│ strstrstrstr │
╞══════╪═════╪═════╪═════╡
│ idabc   │
│ col1132   │
│ col2346   │
└──────┴─────┴─────┴─────┘

You can add slice(1) maybe, but can we add a param like first_column_as_new_header so that everything will be taken care of:

┌──────┬─────┬─────┬─────┐
│ id   ┆ a   ┆ b   ┆ c   │
│ ---  ┆ --- ┆ --- ┆ --- │
│ str  ┆ str ┆ str ┆ str │
╞══════╪═════╪═════╪═════╡
│ col1 ┆ 1   ┆ 3   ┆ 2   │
│ col2 ┆ 3   ┆ 4   ┆ 6   │
└──────┴─────┴─────┴─────┘

This could also potentially useful for transpose a describe table

df.describe().transpose()
shape: (4, 7)
┌──────────┬────────────┬───────────────────┬────────────────────┬──────────┬──────────┬──────────┐
│ column_0column_1column_2column_3column_4column_5column_6 │
│ ---------------------      │
│ strstrstrstrstrstrstr      │
╞══════════╪════════════╪═══════════════════╪════════════════════╪══════════╪══════════╪══════════╡
│ countnull_countmeanstdminmaxmedian   │
│ 30nullnullacnull     │
│ 3.00.02.01.01.03.02.0      │
│ 3.00.04.3333333333333331.52752523165194653.06.04.0      │
└──────────┴────────────┴───────────────────┴────────────────────┴──────────┴──────────┴──────────┘
@stevenlis stevenlis added the enhancement New feature or an improvement of an existing feature label Apr 9, 2023
@cmdlineluser
Copy link
Contributor

Not that it addresses your particular issue but .glimpse() may be of interest if you're not already aware of it.

>>> df.glimpse()
Rows: 3
Columns: 3
$ id   <str> a, b, c
$ col1 <i64> 1, 3, 2
$ col2 <i64> 3, 4, 6
>>> df.describe().glimpse()
Rows: 7
Columns: 4
$ describe <str> count, null_count, mean, std, min, max, median
$ id       <str> 3, 0, None, None, a, c, None
$ col1     <f64> 3.0, 0.0, 2.0, 1.0, 1.0, 3.0, 2.0
$ col2     <f64> 3.0, 0.0, 4.333333333333333, 1.5275252316519465, 3.0, 6.0, 4.0

@stevenlis
Copy link
Author

@cmdlineluser Thanks for mentioning it. It would be nice if it can be read in a table.

@stevenlis
Copy link
Author

Thanks! @magarick
This can be done with:

import polars as pl

pl.DataFrame(
    {"id": ['a', 'b', 'c'],
     "col1": [1, 3, 2],
     "col2": [3, 4, 6]}
).transpose(
    include_header=True, header_name='id', column_names='id'
)

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

2 participants