1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Polars group_by + value_counts

Discussão em 'Python' iniciado por Stack, Setembro 15, 2024.

  1. Stack

    Stack Membro Participativo

    I need some help with polars:

    I have a dataframe with a categorical values column

    ┌───────────────────┬──────────────┬────────┐
    │ session_id ┆ elapsed_time ┆ fqid │
    │ --- ┆ --- ┆ --- │
    │ i64 ┆ i32 ┆ cat │
    ╞═══════════════════╪══════════════╪════════╡
    │ 20090312431273200 ┆ 0 ┆ intro │
    │ 20090312431273200 ┆ 1323 ┆ gramps │
    │ 20090312431273200 ┆ 831 ┆ gramps │
    │ 20090312431273200 ┆ 1147 ┆ gramps │
    │ … ┆ … ┆ … │
    │ 20090312431273200 ┆ 5197 ┆ teddy │
    │ 20090312431273200 ┆ 6180 ┆ teddy │
    │ 20090312431273200 ┆ 7014 ┆ teddy │
    │ 20090312431273200 ┆ 7946 ┆ teddy │
    └───────────────────┴──────────────┴────────┘


    And I want to transform the fqid-column to look like this:

    ┌───────────────────┬─────────────┬────────────┬────────────┐
    │ session_id ┆ fqid_gramps ┆ fqid_intro ┆ fqid_teddy │
    │ --- ┆ --- ┆ --- ┆ --- │
    │ i64 ┆ i32 ┆ i32 ┆ i32 │
    ╞═══════════════════╪═════════════╪════════════╪════════════╡
    │ 20090312431273200 ┆ 1 ┆ 1 ┆ 4 │
    └───────────────────┴─────────────┴────────────┴────────────┘


    That is, I would like to:

    • Group_by over session_id,
    • Make a value_counts() over fqid,
    • Rename columns so that it would be 'fqid_' + category,
    • Turn them into columns (transpose),
    • Add them to the result.

    Technically, I could achieve this without groupby by using something like

    column_values = train['fqid'].value_counts().with_columns(pl.concat_str(pl.lit('fqid' + '_').alias('fqid'), pl.col('fqid').cast(pl.String))).transpose()
    column_values = column_values.rename(column_values.head(1).to_dicts().pop()).slice(1)


    But when I am trying to make an aggregating function from this replacing train['fqid'] with pl.col('fqid') and making a group_by('session_id').aggregate(func('fqid')) it gives me nothing but errors like AttributeError: 'Expr' object has no attribute 'with_columns'.

    Could you kindly suggest a proper way of making this operation?

    Continue reading...

Compartilhe esta Página