python pandas custom agg function

前端 未结 3 1032
情书的邮戳
情书的邮戳 2021-02-05 22:58
Dataframe:
  one two
a  1  x
b  1  y
c  2  y
d  2  z
e  3  z

grp = DataFrame.groupby(\'one\')
grp.agg(lambda x: ???) #or equivalent function

Desired o

3条回答
  •  无人共我
    2021-02-05 23:06

    There is a better way to concatenate strings, in pandas documentation.
    So I prefer this way:

    In [1]: df.groupby('one').agg(lambda x: x.str.cat(sep='|'))
    Out[1]:
         two
    one
    1    x|y
    2    y|z
    3      z
    

提交回复
热议问题