-
Notifications
You must be signed in to change notification settings - Fork 3
/
simple-web-analytics-python-pandas.html
257 lines (217 loc) · 7.32 KB
/
simple-web-analytics-python-pandas.html
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
<!DOCTYPE html>
<html lang="en" itemscope itemtype="http://schema.org/Article">
<head>
<title>Simple web analytics with Python and Pandas</title>
<meta charset="utf-8">
<meta property="og:title" content="Simple web analytics with Python and Pandas">
<meta property="og:site_name" content="Modesto Mas | Blog">
<meta property="og:image" content="https://mmas.github.io/images/profile.jpg">
<meta property="og:image:width" content="200">
<meta property="og:image:height" content="200">
<meta property="og:url" content="https://mmas.github.io/simple-web-analytics-python-pandas">
<meta property="og:locale" content="en_GB">
<meta name="twitter:image" content="https://mmas.github.io/images/profile.jpg">
<meta name="twitter:url" content="https://mmas.github.io/simple-web-analytics-python-pandas">
<meta name="twitter:card" content="summary">
<meta name="twitter:domain" content="mmas.github.io">
<meta name="twitter:title" content="Simple web analytics with Python and Pandas">
<meta name="description" content="We are going to do some analytics with our web visits data. As a simple report we are going to obtain the unique and total visits respect the date and...">
<meta name="twitter:description" content="We are going to do some analytics with our web visits data. As a simple report we are going to obtain the unique and total visits respect the date and...">
<meta property="og:description" content="We are going to do some analytics with our web visits data. As a simple report we are going to obtain the unique and total visits respect the date and...">
<meta name="keywords" content="data-analysis,pandas,python,web-analytics">
<meta property="og:type" content="blog">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta property="og:type" content="article">
<meta property="article:author" content="https://github.com/mmas">
<meta property="article:section" content="data-analysis">
<meta property="article:tag" content="data-analysis,pandas,python,web-analytics">
<meta property="article:published_time" content="2015-02-13">
<meta property="article:modified_time" content="2015-02-13">
<link rel="stylesheet" type="text/css" href="/css/main.css">
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
CommonHTML: {
scale: 93,
showMathMenu: false
},
tex2jax: {
"inlineMath": [["$","$"], ["\\(","\\)"]]
}
});
</script>
<script type="text/javascript" async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-MML-AM_CHTML"></script>
</head>
<body class="entry-detail">
<header>
<div>
<img src="https://mmas.github.io/images/profile.jpg">
<a class="brand" href="/">Modesto Mas</a>
<span>Data/Python/DevOps Engineer</span>
<nav>
<ul>
<li><a href="/tags">Tags</a></li>
<li><a href="https://github.com/mmas/mmas.github.io/issues" target="_blank">Issues</a></li>
</ul>
</nav>
</div>
</header>
<section id="content" role="main">
<article>
<header>
<h1><a href="/simple-web-analytics-python-pandas">Simple web analytics with Python and Pandas</a></h1>
<time datetime="2015-02-13">Feb 13, 2015</time>
<a class="tag" href="/tags?tag=data-analysis">data-analysis</a>
<a class="tag" href="/tags?tag=pandas">pandas</a>
<a class="tag" href="/tags?tag=python">python</a>
<a class="tag" href="/tags?tag=web-analytics">web-analytics</a>
</header>
<aside id="article-nav"></aside>
<section class="body">
<p>We are going to do some analytics with our web visits data. As a simple report we are going to obtain the unique and total visits respect the date and many other paramenters like browser, page wisited, language, operative system...</p>
<h2>Requirements</h2>
<pre>
% pip install python-dateutil pandas
</pre>
<h2>Getting and filtering the data</h2>
<p>Let's assume the structure of our data like:</p>
<table>
<thead>
<tr>
<th>uuid</th>
<th>ip</th>
<th>city</th>
<th>country_code</th>
<th>country_name</th>
<th>language</th>
<th>browser</th>
<th>os</th>
<th>page</th>
<th>date</th>
<th>...</th>
</tr>
</thead>
<tbody>
<tr>
<td>ea2d3169-2b71-4beb-9665-108d302c3a67</td>
<td>78.146.232.107</td>
<td>London</td>
<td>UK</td>
<td>United Kingdom</td>
<td>EN</td>
<td>Firefox</td>
<td>Linux</td>
<td>/foo</td>
<td>2015-02-12 09:25:17.770175</td>
<td>...</td>
</tr>
<tr>
<td>bdb18e99-fc80-4d4b-b4a1-286e67ba374f</td>
<td>95.142.167.120</td>
<td>Paris</td>
<td>FR</td>
<td>France</td>
<td>FR</td>
<td>Safari</td>
<td>Mac</td>
<td>/bar</td>
<td>2015-02-09 21:11:02.134322</td>
<td>...</td>
</tr>
<tr>
<td>...</td>
<td>...</td>
<td>...</td>
<td>...</td>
<td>...</td>
<td>...</td>
<td>...</td>
<td>...</td>
<td>...</td>
<td>...</td>
<td>...</td>
</tr>
</tbody>
</table>
<p>The python code:</p>
<pre>
from datetime import datetime, timedelta
import pandas as pd
source = pd.read_csv('data.csv', index_col='uuid', parse_dates=['date'])
date_1 = datetime.utcnow()
date_0 = date_1 - timedelta(days=30)
data = source[(source['date'] > date_0) & (source['date'] < date_1)]
</pre>
<h2>Aggregating the data</h2>
<p>We'll obtain data structured as:</p>
<table>
<tr>
<td></td>
<th>unique_visits</th>
<th>total_visits</th>
</tr>
<tr>
<th>2015-01-13</th>
<td>90</td>
<td>140</td>
</tr>
<tr>
<th>2015-01-14</th>
<td>104</td>
<td>170</td>
</tr>
<tr>
<th>2015-01-15</th>
<td>80</td>
<td>193</td>
</tr>
<tr>
<th>...</th>
<td>...</td>
<td>...</td>
</tr>
</table>
<table>
<tr>
<td></td>
<th>unique_visits</th>
<th>total_visits</th>
</tr>
<tr>
<th>Linux</th>
<td>76</td>
<td>111</td>
</tr>
<tr>
<th>Mac</th>
<td>101</td>
<td>180</td>
</tr>
<tr>
<th>Windows</th>
<td>40</td>
<td>73</td>
</tr>
</table>
<p>The Python code:</p>
<pre>
def get_visits(groupby):
ip_visits = data.groupby(groupby)['ip']
return pd.DataFrame(
{'unique_visits': ip_visits.apply(lambda x: len(set(x))),
'total_visits': ip_visits.apply(len)})
data['date_day'] = data['date'].apply(datetime.date)
visits_by_date = get_visits('date_day')
# Redefine with date index to avoid lack of dates.
visits_by_date = pd.DataFrame(visits_by_date,
index=pd.date_range(date_0, date_1)).fillna(0)
# Rest of filtered visits.
visits_by_os = get_visits('os')
visits_by_city = get_visits('city')
</pre>
</section>
</article>
</section>
<footer></footer>
<script type="text/javascript" src="/js/article.js"></script>
</body>
</html>