0

Say I have a dataframe like this

import pandas as pd 

test = [
    {1: 434, 2: 343, 3: [592]},
    {1: 434, 2: 343, 3: [192]},
    {1: 534, 2: 743, 3: [392]},
]

df = pd.DataFrame(test)
df

1   2   3
0   434 343 [592]
1   434 343 [192]
2   534 743 [392]

I want to combine rows where columns 2 and 3 are the same, and add up the lists in column 3.

Desired result

1   2   3
0   434 343 [592, 192]
2   534 743 [392]

Attempt so far

I believe group by could be used, and then some sort of aggregation function following it to combine the lists. So something like

df.groupby([1, 2]).aggregate(aggregation_functions)

Though I'm not sure what to put as the aggregation_functions

1 Answer 1

2

You are nearly there. Try:

res = df.groupby([1, 2], as_index=False)[3].sum()

print(res)

     1    2           3
0  434  343  [592, 192]
1  534  743       [392]

If you want to keep the first index for each group [e.g. 0, 2], you can use:

df[3] = df.groupby([1, 2])[3].transform(sum)
df.drop_duplicates(subset=[1, 2], inplace=True)

print(df)

     1    2           3
0  434  343  [592, 192]
2  534  743       [392]
Sign up to request clarification or add additional context in comments.

1 Comment

res = df.groupby([1, 2], as_index=False)[3].sum() is one less copy.

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.