I have two dataframes with different sizes where one is bigger than the other but the second data frame has more columns.
I'm having problems with trying to add a data frame if it has the same column & row value as the other data frame which in this case is id
this is some dummy data and how I was trying to solve it
import pandas as pd
df1 = pd.DataFrame([(1,2,3),(3,4,5),(5,6,7),(7,8,9),(100,10,12),(100,10,12),(100,10,12)], columns=['id','value','c'])
df2 = pd.DataFrame([(1,200,3,4,6),(3,400,3,4,6),(5,600,3,4,6),(5,620,3,4,6)], columns=['id','value','x','y','z'])
so if id of the df1 and df2 are the same then add the column value by the value in "whatToAdd"
data
df1:
id value c
1 2 3
3 4 5
5 6 7
7 8 9
100 10 12
100 10 12
100 10 12
df2:
id value x y z
1 200 3 4 6
3 400 3 4 6
5 600 3 4 6
5 620 3 4 6
expected:
Out:
id value x y z
1 202 3 4 6
3 404 3 4 6
5 606 3 4 6
5 626 3 4 6
tried:
for each in df1.a:
if(df2.loc[df2['a'] == each]):
df2['a']+=df['a']
spew out an error "The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()." which confusing for me cause i tried:
df2.loc[df2['a']==1
out of the loop and it works