csv - Calculate current balance of account in python -
i want complete task in python.
you given sample csv file contains list of transactions.
- the first column account number of person money from,
- the second column amount of money, and
- the third column account number of person money to.
your task parse csv, , calculate current balance of every account number.
submit python script accepts single argument (the location of transactions csv file), , prints out balance of every account.
csv file pattern:
from, amount, 314, 470.21, 275 12, 1788.98, 149 316, 2949.53, 314 5, 2193.48, 454 314, 1402.76, 371 82, 1212.1, 4420
i rendered csv file following code:
import csv def csv_data(path): open(path) csv_file: readcsv = csv.dictreader(csv_file) line in readcsv: col_from = line['from'] col_to = line['to'] col_amount = line['amount'] print(col_from, col_amount, col_to) csv_data('transactions.csv')
how can calculate current balance of every account?
every account made multiple transactions.
for example: if account number 314 in from
column sends given amount account number in to
column. when account number found in from
column previous balance must added.
how can these calculations in for
loop?
this might not ideal answer can use pandas
this.
import pandas pd df = pd.read_csv('/path/to/the/file') total_for_each_account = [df[df['from'] == account]['amount'].sum() account in df.from.unique()]
returns list of sum each unique account present in from
column.
Comments
Post a Comment