0

I have two dataframes df1 and df2. I would like to create a third dataframe that sums elements of the two with the same name.

I tried something like this:

df1 = pd.DataFrame(np.array([['a', 5, 9],['b', 4, 61],['c', 24, 9]]), columns=['name', 'attr1', 'attr2'])

df2 = pd.DataFrame(np.array([['a', 5, 19],['b', 14, 16],['c', 4, 9]]) columns=['name', 'attr1', 'attr2'])

df5=pd.merge(df1, df2, on=['name']).set_index(['name']).sum(axis=1)

But it does not seem to do the job. The new df3 corresponds to the sum of the entire rows while I need an element by element sum.

Mike
  • 375
  • 1
  • 4
  • 14
  • Change sum to `pd.merge(df1, df2, on=['name']).set_index(['name']).apply(''.join,1)` will do the job – BENY Nov 13 '17 at 18:34
  • Thank you. For some reason it doesn't seem to work... – Mike Nov 13 '17 at 18:43
  • Or `pd.merge(df1, df2, on=['name']).set_index(['name']).apply(lambda x : ''.join(x.astype(str)),1)` – BENY Nov 13 '17 at 18:45

0 Answers0