I am trying to subtract the values of a column in DataFrame A, from the values from a column in DataFrame B, but only if multiple column values are equal to each other.
This is why the Index
is so useful, subtraction will be aligned on the indices (both rows and columns).
dfA = dfA.set_index(['Department', 'Speciality', 'TargetMonth'])
dfB = dfB.set_index(['Department', 'Speciality', 'TargetMonth'])
dfA.sub(dfB.rename(columns={'Required': 'Capacity'}), fill_value=0)
Capacity
Department Speciality TargetMonth
IT Servers 2019-1 50
Sales Cars 2019-1 50
2019-2 0
Furniture 2019-1 60