Intellipaat Back

Explore Courses Blog Tutorials Interview Questions

Explore Tech Questions and Answers

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

0 votes
2 views
by (19.9k points)

I am trying to search for the values of DataframeB.ColA in DataframeA.IDCol2 and then create a dataframe with the DataframeA.IDCol1 and the sum of DataframeA.IDCol3 for the values that were matched.

DataframeA

        IDCol1      IDCol2      IDCol3

0       ABC         123         2

1       ABC         456         5

2       ABC         789         2

3       ABC         1011        1

4       CDE         123         3

5       CDE         456         2

6       CDE         CCC         4

7       CDE         AAA         1

DataframeB

        ColA        

0       123     

1       456     

2       CCC     

3       1011      

Output

        Col     Sum     

0       ABC     8       

1       CDE     9  

1 Answer

0 votes
by (25.1k points)

You can use series.isin() and groupby() with sum:

dfA[dfA.IDCol2.isin(dfB.ColA)].groupby('IDCol1')['IDCol3'].sum().reset_index(name='Sum')

Browse Categories

...