I have two DataFrames, like:
df1 = pd.DataFrame([["tom", 1, 2, 3], ["bob", 3, 4, 5], ["ali", 6, 7, 8]], columns=["name", "A", "B", "C"])
df1
Out[44]:
name A B C
0 tom 1 2 3
1 bob 3 4 5
2 ali 6 7 8
df2 = pd.DataFrame([["rob", 1, 2, 3], ["ali", 6, 7, 8]], columns=["name", "A", "B", "D"])
df2
Out[46]:
name A B D
0 rob 1 2 3
1 ali 6 7 8
how can I perform a sum operations to the values with same 'name' and same column, and get a result DataFrame like:
name A B C D
0 tom 1 2 3 NaN # <- tom and bob don't shows up in df2, so the sum is identical
1 bob 3 4 5 NaN # to their values in df1
2 rob 1 2 NaN 3 # <- rob only shows up on df2, so the sum equal to its df2 values
3 ali 12 14 8 8 # <- ali's A and B are sum up, and C and D are identical to their
# corresponding value in df1 and df2
please note, I don't know what names will show up in 'name' columns of both DataFrames.
And, because I have more than two such DataFrames to sum up, how can I do this with all of them in one operation, instead of sum up one by one, if that is possible? Many thanks.