-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython.html
More file actions
327 lines (289 loc) · 12.8 KB
/
Copy pathpython.html
File metadata and controls
327 lines (289 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta content="width=device-width, initial-scale=1.0" name="viewport">
<title>Python Analysis</title>
<link href="./css/styles.css" rel="stylesheet">
<!-- Highlight.js GitHub Dark Theme -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/github-dark.min.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
</head>
<body>
<div class="container">
<header>
<h1>Python Analysis</h1>
</header>
<h2>
Commentary
</h2>
<p>
In this section, calculations were performed based on the metrics specified above using the relevant databases, and the results obtained were interpreted and visualized. All the work was done in Jupyter Notebook, and the script file used for this section can be found
<a href="https://github.com/Projects-of-Data-Analysis/Revenue-Metrics-Analysis/blob/main/notebooks/Revenue%20Metric%20Analysis.ipynb">
here.
</a>
</p>
<h2>
Scripts
</h2>
<section class="analysis">
<h3>Script 1. Import libraries</h3>
<div class="code-image-container">
<pre class="code"><code class="python">
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
</code></pre>
</div>
</section>
<section class="analysis">
<h3>Script 2. Set dark theme for plots</h3>
<div class="code-image-container">
<pre class="code"><code class="python">
plt.style.use("dark_background")
sns.set_palette("bright")
</code></pre>
</div>
</section>
<section class="analysis">
<h3>Script 3. Helper function to format plots</h3>
<div class="code-image-container">
<pre class="code"><code class="python">
def format_plot(title, xlabel, ylabel):
plt.title(title, color='white', fontsize=14)
plt.xlabel(xlabel, color='white', fontsize=12)
plt.ylabel(ylabel, color='white', fontsize=12)
plt.legend(loc='best', fontsize=10)
plt.grid(True, linestyle='--', alpha=0.5)
plt.gca().tick_params(colors='white')
</code></pre>
</div>
</section>
<section class="analysis">
<h3>Script 4: Import games paid users data as users</h3>
<div class="code-image-container">
<pre class="code"><code class="python">
df = pd.read_csv('../data/final_games_dataset.csv')
print(df.dtypes)
print(df.head())
</code></pre>
<img alt="Python Script Result" class="queryImage" src="./images/python/script4.png" style="max-height: 300px; cursor: pointer;">
</div>
</section>
<section class="analysis">
<h3>Script 5: Convert payment_date to datetime</h3>
<div class="code-image-container">
<pre class="code"><code class="python">
df['payment_month'] = pd.to_datetime(df['payment_month'])
</code></pre>
</div>
</section>
<section class="analysis">
<h3>Script 6: Calculating Monthly Recurring Revenue</h3>
<div class="code-image-container">
<pre class="code"><code class="python">
mrr = df[df['status'].isin(['active', 'back','new'])].groupby(df['payment_month'].dt.strftime('%Y-%m'))['total_revenue'].sum()
print("MRR:\n", mrr)
plt.figure(figsize=(12, 6))
plt.bar(mrr.index, mrr.values, label='MRR', width=.4)
format_plot('Monthly Recurring Revenue (MRR) Over Time', 'Month', 'MRR')
plt.show()
</code></pre>
<img alt="Python Script Result" class="queryImage" src="./images/python/script6.png" style="max-height: 300px; cursor: pointer;">
</div>
</section>
<section class="analysis">
<h3>Script 7: Calculating Paid Users</h3>
<div class="code-image-container">
<pre class="code"><code class="python">
paid_users = df[df['total_revenue'] > 0].groupby(df['payment_month'].dt.strftime('%Y-%m'))['user_id'].nunique()
print("Paid Users:\n", paid_users)
plt.figure(figsize=(12, 6))
plt.bar(paid_users.index, paid_users.values, label='Paid Users', width=.4)
format_plot('Paid Users Over Time', 'Month', 'Paid Users')
plt.show()
</code></pre>
<img alt="Python Script Result" class="queryImage" src="./images/python/script7.png" style="max-height: 300px; cursor: pointer;">
</div>
</section>
<section class="analysis">
<h3>Script 8: Calculating Average Revenue Per Paid User (ARPPU)</h3>
<p>
Group payments by month and sum revenue to calculate MRR. The result is rounded to two decimals for
consistency.
</p>
<div class="code-image-container">
<pre class="code"><code class="python">
arppu = (mrr / paid_users).round(2)
print("ARPPU:\n", arppu)
plt.figure(figsize=(12, 6))
plt.bar(arppu.index, arppu.values, label='ARPPU', width=.4)
format_plot('Average Revenue Per Paid User (ARPPU) Over Time', 'Month', 'ARPPU')
plt.show()
</code></pre>
<img alt="Python Script Result" class="queryImage" src="./images/python/script8.png" style="max-height: 300px; cursor: pointer;">
</div>
</section>
<section class="analysis">
<h3>Script 9: Calculating New Paid Users</h3>
<p>
Find the first payment date per user, group by month, and count to get new paid users per month.
</p>
<div class="code-image-container">
<pre class="code"><code class="python">
new_paid_users = df[(df['status'] == 'new') & (df['total_revenue'] > 0)].groupby(df['payment_month'].dt.strftime('%Y-%m'))['user_id'].nunique()
print("Paid Users:", new_paid_users)
plt.figure(figsize=(12, 6))
plt.bar(new_paid_users.index, new_paid_users.values, label='Paid Users', width=.4)
format_plot('New Paid Users', 'Month', 'New Paid Users')
plt.show()
</code></pre>
<img alt="Python Script Result" class="queryImage" src="./images/python/script9.png" style="max-height: 300px; cursor: pointer;">
</div>
</section>
<section class="analysis">
<h3>Script 10: Calculating New MRR</h3>
<p>
Find the first payment date per user, group by month, and sum to get new MRR per month.
</p>
<div class="code-image-container">
<pre class="code"><code class="python">
new_mrr = df[(df['status'] == 'new') & (df['total_revenue'] > 0)].groupby(df['payment_month'].dt.strftime('%Y-%m'))['total_revenue'].sum().round(2)
print('New MRR:\n', new_mrr)
plt.figure(figsize=(12, 6))
plt.bar(new_mrr.index, new_mrr.values, label='New MRR', width=.4)
format_plot('New Monthly Recurring Revenue (MRR)', 'Month', 'USD')
plt.show()
</code></pre>
<img alt="Python Script Result" class="queryImage" src="./images/python/script10.png" style="max-height: 300px; cursor: pointer;">
</div>
</section>
<section class="analysis">
<h3>Script 11: Calculating Churned Users</h3>
<p>
Find the last payment date per user, group by month, and count to get churned users per month.
</p>
<div class="code-image-container">
<pre class="code"><code class="python">
churned_users = df[df['status'] == 'churn'].groupby(df['payment_month'].dt.strftime('%Y-%m'))['user_id'].nunique()
print("Churned Users:\n", churned_users)
plt.figure(figsize=(12, 6))
plt.bar(churned_users.index, churned_users.values, label='Churned Users', width=.4)
format_plot('Churned Users Over Time', 'Month', 'Churned Users')
plt.show()
</code></pre>
<img alt="Python Script Result" class="queryImage" src="./images/python/script11.png" style="max-height: 300px; cursor: pointer;">
</div>
</section>
<section class="analysis">
<h3>Script 12: Calculating Churned Rate</h3>
<p>
</p>
<div class="code-image-container">
<pre class="code"><code class="python">
paid_users_shifted = paid_users.shift(1)
churn_rate = (churned_users / paid_users_shifted).round(2)
print("Churn Rate:\n", churn_rate)
plt.figure(figsize=(12, 6))
plt.bar(churn_rate.index, churn_rate.values, label='Churn Rate', width=.4)
format_plot('Churn Rate Over Time', 'Month', 'Churn Rate')
plt.show()
</code></pre>
<img alt="Python Script Result" class="queryImage" src="./images/python/script12.png" style="max-height: 300px; cursor: pointer;">
</div>
</section>
<section class="analysis">
<h3>Script 13: Calculating Churned Revenue</h3>
<p>
</p>
<div class="code-image-container">
<pre class="code"><code class="python">
churned_revenue = df[df['status'] == 'churn'].groupby(df['payment_month'].dt.strftime('%Y-%m'))['total_revenue_previous'].sum().round(2)
print("Churned Revenue:\n", churned_revenue)
plt.figure(figsize=(12, 6))
plt.bar(churned_revenue.index, churned_revenue.values, label='Churned Revenue', width=.4)
format_plot('Churned Revenue Over Time', 'Month', 'USD')
plt.show()
</code></pre>
<img alt="Python Script Result" class="queryImage" src="./images/python/script13.png" style="max-height: 300px; cursor: pointer;">
</div>
</section>
<section class="analysis">
<h3>Script 14: Revenue Churned Rate</h3>
<p>
</p>
<div class="code-image-container">
<pre class="code"><code class="python">
mrr_shifted = mrr.shift(1)
revenue_churn_rate = (churned_revenue / mrr_shifted).round(2)
print("Revenue Churn Rate:\n", revenue_churn_rate)
plt.figure(figsize=(12, 6))
plt.bar(revenue_churn_rate.index, revenue_churn_rate.values, label='Revenue Churn Rate', width=.4)
format_plot('Revenue Churn Rate Over Time', 'Month', 'Rate')
plt.show()
</code></pre>
<img alt="Python Script Result" class="queryImage" src="./images/python/script14.png" style="max-height: 300px; cursor: pointer;">
</div>
</section>
<section class="analysis">
<h3>Script 15: Calculating Expension MRR</h3>
<p>
For churned users, sum their revenue from the previous month to calculate churned revenue.
</p>
<div class="code-image-container">
<pre class="code"><code class="python">
expansion_mrr = df[(df['status'] == 'active') & (df['total_revenue'] > df['total_revenue_previous'])].groupby(df['payment_month'].dt.strftime('%Y-%m')).apply(lambda x: (x['total_revenue'] - x['total_revenue_previous']).sum()).round(2)
print("Expansion MRR:\n", expansion_mrr)
plt.figure(figsize=(12, 6))
plt.bar(expansion_mrr.index, expansion_mrr.values, label='Expansion MRR', width=.4)
format_plot('Expansion MRR Over Time', 'Month', 'USD')
plt.show()
</code></pre>
<img alt="Python Script Result" class="queryImage" src="./images/python/script15.png" style="max-height: 300px; cursor: pointer;">
</div>
</section>
<section class="analysis">
<h3>Script 16: Calculating Contraction MRR</h3>
<p>
</p>
<div class="code-image-container">
<pre class="code"><code class="python">
contraction_mrr = df[(df['status'] == 'active') & (df['total_revenue'] < df['total_revenue_previous'])].groupby(df['payment_month'].dt.strftime('%Y-%m')).apply(lambda x: (x['total_revenue'] - x['total_revenue_previous']).sum()) print("Contraction MRR:\n", contraction_mrr) plt.figure(figsize=(12, 6)) plt.bar(contraction_mrr.index, contraction_mrr.values, label='Contraction MRR' , width=.4) format_plot('Contraction MRR Over Time', 'Month' , 'USD' ) plt.show()
</code></pre>
<img alt="Python Script Result" class="queryImage" src="./images/python/script16.png" style="max-height: 300px; cursor: pointer;">
</div>
</section>
</div>
<!-- Modal -->
<script>
document.addEventListener("DOMContentLoaded", function () {
const modal = document.getElementById("imageModal");
const modalImg = document.getElementById("modalImg");
const span = document.getElementsByClassName("close")[0];
document.querySelectorAll(".queryImage").forEach(img => {
img.addEventListener("click", () => {
modal.style.display = "block";
modalImg.src = img.src;
});
});
span.onclick = function () {
modal.style.display = "none";
}
window.onclick = function (event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
});
</script>
<div class="modal" id="imageModal">
<span class="close">×</span>
<img class="modal-content" id="modalImg">
</div>
<script>
window.addEventListener("load", function () {
hljs.highlightAll();
});
</script>
</body>
</html>