How to determine the balance associated with a given BTC address based on input/output amounts
It looks like you are trying to calculate address balances by what I assume to be transactions. The way the data is presented in your table, I get the impression that you are assuming that there is only one input address and one output address in every transaction. That's not the case, though.
Each input in a transaction spends an Unspent Transaction Output (UTXO) created by a previous transaction. Transactions can have many inputs and there is no requirement for all the inputs to have been received to the same address. In fact, address reuse is not recommended, so most of the time every input is associated with a different address.
Let's look for example at this random transaction (via blockchair.com) with four inputs and two outputs:

It uses funds that were previously received to four different addresses and reassigns the amounts to two new addresses. So, you'll need to amend your approach slightly: instead of looking at transactions as atomic units to feed your script, parse each input and each output separately.