Advanced Python Data Science Exercise
Set
1. Matplotlib: Advanced OHLC Candlestick Plot with Volume Annotations
Using the provided OHLC dataset, plot candlestick-style OHLC bars. Highlight the top 3
highest volume days with annotations. Ensure text size dynamically adjusts to prevent
overlaps.
Dataset Preparation Code:
import pandas as pd
import numpy as np
[Link](42)
dates = pd.date_range("2023-01-01", periods=100)
ohlc_df = [Link]({
'Date': dates,
'Open': [Link](100, 200, 100).round(2),
'High': [Link](200, 300, 100).round(2),
'Low': [Link](50, 100, 100).round(2),
'Close': [Link](100, 200, 100).round(2),
'Volume': [Link](1_000, 10_000, 100)
})
2. Matplotlib: Multi-Axis Climate Plot with Interactive Hover
Plot Temperature, Humidity, and WindSpeed on a shared x-axis with separate y-axes. Add
hover interactivity using mpl_connect or mplcursors.
Dataset Preparation Code:
climate_df = [Link]({
'Date': pd.date_range("2023-01-01", periods=100),
'Temperature': [Link](20, 40, 100),
'Humidity': [Link](40, 90, 100),
'WindSpeed': [Link](5, 30, 100)
})
3. Plotly: Drilldown Sunburst with Time-Series Update
Create a Plotly Dash app with a sunburst chart for Region > Country > Product > Quarter. On
clicking a segment, show a corresponding time series.
Dataset Preparation Code:
regions = ['Asia', 'Europe', 'America']
countries = {'Asia': ['India', 'China'], 'Europe': ['France', 'Germany'], 'America': ['USA',
'Brazil']}
products = ['A', 'B', 'C']
quarters = ['Q1', 'Q2', 'Q3', 'Q4']
data = []
for region in regions:
for country in countries[region]:
for product in products:
for quarter in quarters:
[Link]({
'Region': region,
'Country': country,
'Product': product,
'Quarter': quarter,
'Sales': [Link](1000, 10000)
})
sales_df = [Link](data)
4. Plotly: Linked Hover and Animated Subplots
Use Plotly to plot: (a) a choropleth map, (b) a scatter plot, and (c) a time-series bar chart.
Animate monthly data and enable linked hover.
Dataset Preparation Code:
world_df = [Link]({
'Country': ['USA', 'India', 'Germany', 'Brazil', 'China'],
'Sales': [Link](10_000, 100_000, 5),
'ISO': ['USA', 'IND', 'DEU', 'BRA', 'CHN']
})
product_df = [Link]({
'Product': ['A', 'B', 'C', 'D'],
'Price': [Link](10, 100, 4),
'Volume': [Link](100, 500, 4)
})
time_series_df = [Link]({
'Month': pd.date_range("2023-01-01", periods=12, freq='M'),
'Sales': [Link](5000, 15000, 12)
})
5. Pandas: Rolling Average Anomaly Detection
From a MultiIndex dataset with transaction logs, compute rolling averages and flag spend
increase anomalies.
Dataset Preparation Code:
user_ids = [f'U{i}' for i in range(1, 21)]
dates = pd.date_range('2023-01-01', '2023-04-30')
transaction_data = []
for uid in user_ids:
for date in [Link](dates, 40):
transaction_data.append({
'UserID': uid,
'Date': date,
'Spend': round([Link](10, 500), 2)
})
transaction_df = [Link](transaction_data)
transaction_df = transaction_df.sort_values(['UserID', 'Date']).set_index(['UserID', 'Date'])
6. Pandas: Funnel Analysis from Multi-source Data
Using 3 CSVs (users, logins, purchases), compute user funnel conversion metrics based on
time windows.
Dataset Preparation Code:
user_ids = [f'U{i}' for i in range(1, 21)]
users_df = [Link]({
'user_id': user_ids,
'join_date': pd.date_range('2023-01-01', periods=20)
})
logins_df = [Link]({
'user_id': [Link](user_ids, 50),
'login_date': pd.date_range('2023-01-01', periods=50)
})
purchases_df = [Link]({
'user_id': [Link](user_ids, 30),
'purchase_date': pd.date_range('2023-01-10', periods=30),
'amount': [Link](100, 1000, 30)
})
7. NumPy: Memory-Efficient Weighted Window Function
Apply a custom window function to a large 1D NumPy array (>10 million elements) using
broadcasting (no loops).
Dataset Preparation Code:
large_array = [Link](10_000_000)
# Goal: Apply custom weighted moving average of size 5
8. NumPy: Vectorized Random Walks with Reset Constraint
Simulate 100,000 random walks of 1000 steps. Reset to zero if walk drops below -10. Track
resets and final position.
Dataset Preparation Code:
num_walks = 100_000
steps = 1000
random_walks = [Link]([-1, 1], size=(num_walks, steps))