Mathew K Analytics

Lesson 34 · Python for Banking and Finance

Automating Banking Workflows with Python: Practical Training for Financial Efficiency

This lesson will teach you how to use Python to automate repetitive banking tasks. Automation is important in banking because it improves efficiency,…

⬇ Download notebookOpen in Colab ↗

📓 Full notebook

Download .ipynb

Automating Banking Workflows with Python#

  • This lesson will teach you how to use Python to automate repetitive banking tasks.
  • Automation is important in banking because it improves efficiency, reduces errors, and allows staff to focus on high-value work.
  • You will learn to organize transaction data, detect suspicious activity, and streamline operations using common Python patterns.
  • By the end, you will be able to build simple automation scripts and understand how banks use automation daily.
import pandas as pd
import numpy as np
import warnings
warnings.filterwarnings('ignore')

Understanding the Data for Banking Automation#

  • Each dataset in banking represents different aspects, such as transactions, customers, or account types.
  • Data tables are usually structured with rows (records) and columns (fields/features).
  • Beginners often make mistakes like: using the wrong column as unique ID, forgetting to check for duplicates, or mixing up datatypes (dates vs strings).
  • Carefully explore your datasets before building automations to avoid costly mistakes later.
np.random.seed(42)
n_transactions = 1000
n_customers = 200
df = pd.DataFrame({
    'transaction_id': range(1, n_transactions + 1),
    'customer_id': np.random.choice([f'CUST_{i:04d}' for i in range(1, n_customers + 1)], n_transactions),
    'amount': np.round(np.random.normal(150, 60, n_transactions), 2),
    'transaction_type': np.random.choice(['Debit', 'Credit'], n_transactions),
    'channel': np.random.choice(['ATM', 'Online', 'Branch', 'POS'], n_transactions),
    'date': pd.date_range(start='2024-01-01', periods=n_transactions, freq='h')
})
print(df.shape)
print(df.head(3))
(1000, 6)
   transaction_id customer_id  amount transaction_type channel  \
0               1   CUST_0103  238.77           Credit     ATM   
1               2   CUST_0180  269.17           Credit     POS   
2               3   CUST_0093   58.62           Credit  Online   

                 date  
0 2024-01-01 00:00:00  
1 2024-01-01 01:00:00  
2 2024-01-01 02:00:00  
customer_ids = [f'CUST_{i:04d}' for i in range(1, 201)]
customers = pd.DataFrame({
    'customer_id': customer_ids,
    'segment': ['Retail'] * 150 + ['Business'] * 50,
    'region': ['Metro'] * 100 + ['Regional'] * 100
})
print(customers.shape)
print(customers.head(3))
(200, 3)
  customer_id segment region
0   CUST_0001  Retail  Metro
1   CUST_0002  Retail  Metro
2   CUST_0003  Retail  Metro
np.random.seed(42)
accounts = pd.DataFrame({
    'account_id': [f'ACC_{i:05d}' for i in range(1, 201)],
    'customer_id': customer_ids,
    'account_type': np.random.choice(['Savings', 'Cheque', 'Credit'], size=200),
    'open_date': pd.date_range(start='2015-01-01', periods=200, freq='30D')
})
print(accounts.shape)
print(accounts.head(3))
(200, 4)
  account_id customer_id account_type  open_date
0  ACC_00001   CUST_0001       Credit 2015-01-01
1  ACC_00002   CUST_0002      Savings 2015-01-31
2  ACC_00003   CUST_0003       Credit 2015-03-02

Beginner Example 1: Filter High-Value Transactions#

  • Learn to select transactions above a certain value.
  • Filtering is a simple automation that supports fraud detection and reporting.
high_value = df[df['amount'] > 250]
print(high_value.head(3))
print(f'Total high-value transactions: {len(high_value)}')
    transaction_id customer_id  amount transaction_type channel  \
1                2   CUST_0180  269.17           Credit     POS   
14              15   CUST_0104  257.71           Credit     ATM   
19              20   CUST_0002  307.00           Credit     ATM   

                  date  
1  2024-01-01 01:00:00  
14 2024-01-01 14:00:00  
19 2024-01-01 19:00:00  
Total high-value transactions: 54

Beginner Example 2: Select Online Transactions#

  • Isolate transactions performed through the 'Online' channel.
  • This helps business teams analyze digital banking trends.
online_txns = df[df['channel'] == 'Online']
print(online_txns.head(3))
print(f'Number of online transactions: {len(online_txns)}')
   transaction_id customer_id  amount transaction_type channel  \
2               3   CUST_0093   58.62           Credit  Online   
4               5   CUST_0107  163.56           Credit  Online   
5               6   CUST_0072  200.38            Debit  Online   

                 date  
2 2024-01-01 02:00:00  
4 2024-01-01 04:00:00  
5 2024-01-01 05:00:00  
Number of online transactions: 236

Beginner Example 3: Count Transactions per Customer#

  • Count total transactions for each customer.
  • Summarizing by customer supports personalized offers or flagging inactive accounts.
txn_count = df.groupby('customer_id').size().reset_index(name='txn_count')
print(txn_count.head(3))
  customer_id  txn_count
0   CUST_0001          8
1   CUST_0002          5
2   CUST_0003          6

Intermediate Example 1: Merge Transactions and Customer Data#

  • Enrich transaction logs with customer segment and region information.
  • Merging datasets is a key banking automation for reporting or compliance.
merged = pd.merge(df, customers, on='customer_id', how='left')
print(merged.head(3))
   transaction_id customer_id  amount transaction_type channel  \
0               1   CUST_0103  238.77           Credit     ATM   
1               2   CUST_0180  269.17           Credit     POS   
2               3   CUST_0093   58.62           Credit  Online   

                 date   segment    region  
0 2024-01-01 00:00:00    Retail  Regional  
1 2024-01-01 01:00:00  Business  Regional  
2 2024-01-01 02:00:00    Retail     Metro  

Intermediate Example 2: Flag Suspicious Large Debits#

  • Automate the detection of large debit transactions for monitoring fraud risks.
  • This enables proactive alerts and compliance.
merged['suspicious_flag'] = np.where((merged['amount'] > 400) & (merged['transaction_type'] == 'Debit'), 1, 0)
print(merged[['transaction_id', 'amount', 'transaction_type', 'suspicious_flag']].head(5))
print(f'Suspicious transactions detected: {merged.suspicious_flag.sum()}')
   transaction_id  amount transaction_type  suspicious_flag
0               1  238.77           Credit                0
1               2  269.17           Credit                0
2               3   58.62           Credit                0
3               4   81.85            Debit                0
4               5  163.56           Credit                0
Suspicious transactions detected: 0

Intermediate Example 3: Automate Daily Transaction Summary#

  • Automate daily summary reports by aggregating transaction amounts and counts by day.
  • Daily reporting reduces manual work and errors.
df['date_only'] = df['date'].dt.date
daily_summary = df.groupby('date_only').agg({'amount':['sum','count']})
daily_summary.columns = ['total_amount', 'txn_count']
print(daily_summary.head(3))
            total_amount  txn_count
date_only                          
2024-01-01       3771.76         24
2024-01-02       3993.85         24
2024-01-03       3777.92         24

Advanced Example 1: Batch Export Suspicious Transactions#

  • Automate exporting a CSV of flagged suspicious transactions for compliance reviews.
  • Exporting is a key part of workflow automation in banking.
suspicious_txns = merged[merged['suspicious_flag'] == 1]
suspicious_txns.to_csv('suspicious_transactions.csv', index=False)

Advanced Example 2: Automate Report Distribution via Email (Simulated)#

  • Although we will not send real emails, automating report saving prepares data for downstream tasks.
  • In real banking workflows, you often automate emailing reports to compliance officers.
# Placeholder for future email automation. Here we simulate report preparation.
with open('report_ready.txt', 'w') as f:
    f.write('Compliance report is ready for distribution.')

Great work completing the automation lesson!#

  • Practice by applying these steps to your own datasets.
  • For step-by-step videos, search for 'Banking Data Automation Python' on YouTube.
  • You are now ready to tackle real-world banking automation scenarios!

Found this useful?

All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.