Lesson 6 · Python for Banking and Finance
How to Load and Analyze Bank Datasets in Python for Financial Insights
In this lesson, we will learn how to load and explore real banking datasets with Python. Bank datasets are crucial for understanding customer behavior,…
- CoursePython for Banking and Finance
- Lesson6 of 24
- Video24 min
- FormatJupyter notebook · 24 code cells
What you'll learn
Data
No separate download needed — the notebook creates or downloads everything it uses.
📓 Full notebook
Download .ipynbLoading and Exploring Bank Datasets#
- In this lesson, we will learn how to load and explore real banking datasets with Python.
- Bank datasets are crucial for understanding customer behavior, detecting fraud, and managing risk.
- You will build skills to read datasets, understand structure, spot problems, and prepare for real analysis.
- We will use practical examples you could face in a real bank data science job.
import pandas as pd
import numpy as np
import warnings
warnings.filterwarnings('ignore')
Understanding Banking Data Structures#
- Banking datasets are usually tables with rows for transactions, accounts, or customers.
- Columns describe each row, like amount, type, customer ID, or date.
- Data might be synthetic or real, but structure is similar in practice.
- Beginners often confuse row meaning or misuse data types (for example, dates as strings).
- Missing or invalid data, duplicate rows, and mixed column types are common mistakes.
# Example 1: Create synthetic banking transactions dataset
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))
# Example 2: Create a simple customers table
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))
# Example 3: Create a synthetic account types table
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))
# Example 4: Load the German Credit Risk dataset from web
url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/statlog/german/german.data'
cols = [f'feature_{i}' for i in range(1, 25)] + ['credit_status']
credit_df = pd.read_csv(url, sep=' ', names=cols)
credit_df['default_flag'] = (credit_df['credit_status'] == 2).astype(int)
print(credit_df.shape)
print(credit_df[['credit_status', 'default_flag']].head(3))
# Example 5: Load a public credit card fraud dataset
cc_url = 'https://storage.googleapis.com/download.tensorflow.org/data/creditcard.csv'
fraud_df = pd.read_csv(cc_url)
print(fraud_df.shape)
print(fraud_df.head(3))
# Example 6: Check data types, missing values, and basic stats
print(fraud_df.dtypes)
print(fraud_df.isnull().sum().head())
print(fraud_df.describe().T.head(5))
# Example 7: Explore unique values in key columns
print(df['channel'].unique())
print(df['transaction_type'].value_counts())
# Example 8: Filter for large credit transactions at ATMs
atm_credits = df[(df['channel'] == 'ATM') & (df['transaction_type'] == 'Credit') & (df['amount'] > 300)]
print(atm_credits.shape)
print(atm_credits[['transaction_id', 'amount', 'channel', 'transaction_type']].head())
# Example 9: Merge transactions with customer segments and regions
merged = df.merge(customers, on='customer_id', how='left')
print(merged.head(3)[['transaction_id', 'customer_id', 'segment', 'region']])
# Example 10: Group transaction amount statistics by customer segment
grouped_stats = merged.groupby('segment')['amount'].agg(['count', 'mean', 'std', 'min', 'max'])
print(grouped_stats)
# Example 11: Find customers with only one account type
account_counts = accounts.groupby('customer_id')['account_type'].nunique()
single_account_customers = account_counts[account_counts == 1]
print(single_account_customers.shape)
print(single_account_customers.head())
# Example 12: Parse transaction dates and sort
df['date'] = pd.to_datetime(df['date'])
sorted_txn = df.sort_values('date').reset_index(drop=True)
print(sorted_txn[['transaction_id', 'date']].head(3))
# Example 13: Find possible duplicate transactions
duplicates = df[df.duplicated(['customer_id', 'amount', 'date', 'transaction_type'], keep=False)]
print(duplicates[['transaction_id', 'customer_id', 'amount', 'transaction_type', 'date']].head())
# Example 14: Handle missing values in banking data
fraud_df_missing = fraud_df.copy()
fraud_df_missing.loc[0, 'Amount'] = np.nan # Injecting a missing value
filled = fraud_df_missing['Amount'].fillna(fraud_df_missing['Amount'].mean())
print(filled.head(3))
# Example 15: Catch errors when loading files
try:
pd.read_csv('non_existent_file.csv')
except FileNotFoundError as e:
print('File not found. Please check the path:', e)
# Example 16: Validate column types before analysis
if not np.issubdtype(df['amount'].dtype, np.number):
print('Amount column is not numeric!')
else:
print('Amount column is numeric, safe for stats calculations.')
# Example 17: Always copy DataFrames before modifying
safe_copy = df.copy()
safe_copy['amount'] = safe_copy['amount'] * 1.1 # Simulate a processing step
print(safe_copy['amount'].head(3))
# Example 18: Use describe(include='all') for broad summary
print(df.describe(include='all').T)
# Example 19: End-to-end: Flag risky transactions (amount > 3 std above segment mean)
thresholds = merged.groupby('segment')['amount'].agg(['mean', 'std'])
merged = merged.join(thresholds, on='segment', rsuffix='_stats')
merged['is_risky'] = merged['amount'] > (merged['mean'] + 3*merged['std'])
print(merged[['transaction_id', 'segment', 'amount', 'mean', 'std', 'is_risky']].head(8))
print('Total risky transactions:', merged['is_risky'].sum())
Keep Building!#
- Ready to try more? Search YouTube for 'Python pandas banking data analysis' for hands-on videos.
- To keep your skills sharp, try building your own synthetic financial dataset and share your work online.
# Example 20: List all columns containing the word 'flag'
flag_cols = [col for col in merged.columns if 'flag' in col]
print(flag_cols)
# Example 21: Extract month from transaction dates and count
df['month'] = df['date'].dt.month
print(df['month'].value_counts().sort_index())
# Example 22: Save merged banking dataset to CSV file
merged.to_csv('bank_transactions_with_segments.csv', index=False)
print('File saved!')
Found this useful?
All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.



