1
votes

I have made two data frame

df1=pd.read_csv()
df2=pd.read_csv()

df1

**A     B   C**

  Jhon  3   4

  David 3   6

df2

**A     B   C**

  leu   3   4

  Jhon  7   6
  David 6   8

I want to do like, if the name column(A) of df1 is match with name column(A) of df2, then the matched row of df2 will provide summation of total number of column B of df2. finally my result will be like :

total_matchedValueOf_B= 7+6=13.
2

2 Answers

0
votes

You could use isin() method and boolean indexing:

df2.loc[df2['A'].isin(df1['A']), 'B'].sum()
0
votes

Use df.merge:

In [1642]: result = df1.merge(df2, on='A')['B_y'].sum()

In [1643]: result
Out[1643]: 13