Home > other >  How to modify a dataframe grouping columns values in a list
How to modify a dataframe grouping columns values in a list

Time:09-28

I have a data frame:

df = pd.DataFrame({'comment':['any comment','any_comment','any comment','any comment','another comment','another comment'], 'key':[1,11,13,113,123,300]})

output:

comment                   key
any comment                1
any comment                11
any comment                13
any comment                113
any comment                12
another comment            123
another comment            300

And I want to modify the above dataframe to get this output:

comment                         key
any comment                [1,11,13,113,12]
another comment            [123,300]

The key columns now is a list. Whatś the best way to do it?

CodePudding user response:

You can do:

import pandas as pd

df.groupby('comment')['key'].apply(list)

comment
another comment          [123, 300]
any comment        [1, 11, 13, 113]
Name: key, dtype: object

CodePudding user response:

df.groupby('comment')['key'].agg(list)
comment
another comment          [123, 300]
any comment        [1, 11, 13, 113]
Name: key, dtype: object

CodePudding user response:

Another way - using comprehensions -

print(*((k, list(v['key'])) for k, v in df.groupby(['comment'])))

Output

('another comment', [123, 300]) ('any comment', [1, 13, 113]) ('any_comment', [11])
  • Related