My question is an expansion of the one answered here:
Adding two pandas dataframes
Assuming the same dataframes but with a new column containing strings.
import pandas as pd
df1 = pd.DataFrame([('Dog',1,2),('Cat',3,4),('Rabbit',5,6)], columns=['Animal','a','b'])
df2 = pd.DataFrame([('Dog',100,200),('Cat',300,400),('Rabbit',500,600)], columns=['Animal','a','b']
Using the solution would create this:
df_add = df1.add(df2, fill_value=0)
Out:
Animal a b
0 DogDog 101 202
1 CatCat 303 404
2 RabbitRabbit 505 606
A potential solution could be just to index the Animal column and then run the .add function and then unindex the animal column again. But is there a more simple way that just adjusts this formula df_add = df1.add(df2, fill_value=0)so that the following solution is given:
Out:
Animal a b
0 Dog 101 202
1 Cat 303 404
2 Rabbit 505 606
I tried df_add.iloc[:,1:] = df1.iloc[:,1:].add(df2.iloc[:,1:], fill_value=0) and it did not work.
1