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] group_by with polars concatenating values

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

  1. Stack

    Stack Membro Participativo

    I have a polars dataframe that I want to group by and concatenate the unique values in as a single entry.

    in pandas, I go:

    def unique_colun_values(x):
    return('|'.join(set(x)))

    dd=pd.DataFrame({'col1':[1,1,2,3,4],'col2':['a','a','a','b','b'],'col3':['qwe','rty','asd','fgh','zxc']})
    dd.groupby('col1').agg(lambda x: unique_colun_values(x))


    This works fine

    when I try to implement in polars:

    pl.from_pandas(dd).group_by('col1').agg(lambda x: unique_colun_values(x), allow_object=True)


    I get the following error:

    TypeError: cannot create expression literal for value of type function.

    Hint: Pass `allow_object=True` to accept any value and create a literal of type Object.


    Am I missing something?

    Continue reading...

Compartilhe esta Página