5

In order to "concatenate" a few rows to 1 list with groupby in Pandas, I can do this:

df = pd.DataFrame({'A': [1,1,2,2,2,2,3],'B':['a','b','c','d','e','f','g']})

df = df.groupby('A')['B'].apply(list)

I will get:

A
-------------------
1          [a, b]
2    [c, d, e, f]
3             [g]

I want to do the same with agg:

f = {"B":[list]}
df = df.groupby('A').agg(f)

that gives errors, any idea?

Thanks,

1 Answer 1

10

You can use tolist - output is Series:

df = df.groupby('A')['B'].agg(lambda x: x.tolist())
print (df)
A
1          [a, b]
2    [c, d, e, f]
3             [g]
dtype: object

Or with define column B in dict - output is DataFrame:

df = df.groupby('A').agg({'B': lambda x: x.tolist()})
print (df)
              B
A              
1        [a, b]
2  [c, d, e, f]
3           [g]

Also works:

df = df.groupby('A')['B'].agg(lambda x: list(x))
print (df)
A
1          [a, b]
2    [c, d, e, f]
3             [g]
dtype: object

df = df.groupby('A').agg({'B': lambda x: list(x)})
print (df)
              B
A              
1        [a, b]
2  [c, d, e, f]
3           [g]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.