I would like to add the two dataframes together as column 1 is added to column 1 (as in matrix summation based on i, j), column 2 is added to column 2 in case that the column does not exist in one of the dataframes, they should still appended from one of the dataframes.
The output should be a dataframe consisting an shown index of: ['Sun', 'Wind', 'Water', 'Flow'] then the dataframe should be ranging from 1:22.
All values are currently 0, but if column "2", cell 3 in dt1 is 200, then this cell is added to column "2" cell 3 in dt2 which is 10 for the total of 210.
import pandas as pd
cols = range(1, 20)
idx = ['Sun', 'Wind', 'Water', 'Flow']
rows = [0] * int(len(cols))
rows = [rows]
dt1 = pd.DataFrame(rows, index=idx, columns=cols)
dt1 = dt1.reset_index()
cols = range(3, 22)
idx = ['Sun', 'Wind', 'Water', 'Flow']
rows = [0] * int(len(cols))
rows = [rows]
dt2 = pd.DataFrame(rows, index=idx, columns=cols)
dt2 = dt2.reset_index()
TRIED:
df = dt1[dt1.columns[1:]].add(dt2[dt2.columns[1:]]).fillna(0)
It may be that matrix addition is the way forward with two for loops, however, I'm not quite sure how to handle the comparison of appending the right values in the right columns.
dt.reset_index(), pandas can add dataframes which have an index, and also you wouldn't need to slice[1:]to avoid the index. so keep the index as-is.