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 equivalent of pandas expression...

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

  1. Stack

    Stack Membro Participativo

    pandasdf=pd.DataFrame(
    {
    "A": [1, 2, 3, 4, 5],
    "fruits": ["banana", "banana", "apple", "apple", "banana"],
    "B": [5, 4, 3, 2, 1],
    "cars": ["beetle", "audi", "beetle", "beetle", "beetle"],
    "optional": [28, 300, None, 2, -30],
    }
    )
    pandasdf.groupby(["fruits","cars"])['B'].sum().unstack()


    [​IMG]

    How can i create an equivalent truth table in polars?

    Something like the below table into a truth table

    df=pl.DataFrame(
    {
    "A": [1, 2, 3, 4, 5],
    "fruits": ["banana", "banana", "apple", "apple", "banana"],
    "B": [5, 4, 3, 2, 1],
    "cars": ["beetle", "audi", "beetle", "beetle", "beetle"],
    "optional": [28, 300, None, 2, -30],
    }
    )
    df.group_by("fruits","cars").agg(pl.col('B').sum()) #->truthtable


    The efficiency of the code is important as the dataset is too large (for using it with apriori algorithm)

    The unstack function in polars is different, polars alterative for pd.crosstab would also work.

    Continue reading...

Compartilhe esta Página