python - Replicating GROUP_CONCAT for pandas.DataFrame -


i have pandas dataframe df:

+------+---------+   | team | user    |   +------+---------+   |    | elmer   |   |    | daffy   |   |    | bugs    |   | b    | dawg    |   |    | foghorn |   | b    | speedy  |   |    | goofy   |   |    | marvin  |   | b    | pepe    |   | c    | petunia |   | c    | porky   |   +------+---------   

i want find or write function return dataframe return in mysql using following:

select   team,   group_concat(user)   df group   team 

for following result:

+------+---------------------------------------+   | team | group_concat(user)                    |   +------+---------------------------------------+   |    | elmer,daffy,bugs,foghorn,goofy,marvin |   | b    | dawg,speedy,pepe                      |   | c    | petunia,porky                         |   +------+---------------------------------------+   

i can think of nasty ways iterating on rows , adding dictionary, there's got better way.

do following:

df.groupby('team').apply(lambda x: ','.join(x.user)) 

to series of strings or

df.groupby('team').apply(lambda x: list(x.user)) 

to series of lists of strings.

here's results like:

in [33]: df.groupby('team').apply(lambda x: ', '.join(x.user)) out[33]: team       elmer, daffy, bugs, foghorn, goofy, marvin b                               dawg, speedy, pepe c                                   petunia, porky dtype: object  in [34]: df.groupby('team').apply(lambda x: list(x.user)) out[34]: team       [elmer, daffy, bugs, foghorn, goofy, marvin] b                               [dawg, speedy, pepe] c                                   [petunia, porky] dtype: object 

note in general further operations on these types of series slow , discouraged. if there's way aggregate without putting list inside of series should consider using approach instead.


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -