-
Notifications
You must be signed in to change notification settings - Fork 0
/
Uber Data Analysis April 2014.py
189 lines (97 loc) · 2.67 KB
/
Uber Data Analysis April 2014.py
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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
get_ipython().run_line_magic('pylab', 'inline')
import pandas
import seaborn
# # Load CSV file into memory
#
# In[2]:
data=pandas.read_csv('Desktop/uber-raw-data-apr14.csv')
data.head()
# # Convert DateTime and add some useful columns
#
# In[ ]:
data['Date/Time'] = data['Date/Time'].map(pandas.to_datetime)
# In[5]:
data.tail()
# In[6]:
def get_dom(dt):
return dt.day
data['dom'] = data['Date/Time'].map(get_dom)
data.tail()
# In[8]:
def get_weekday(dt):
return dt.weekday()
data['weekday'] = data['Date/Time'].map(get_weekday)
data.tail()
# In[9]:
def get_hour(dt):
return dt.hour
data['hour'] = data['Date/Time'].map(get_hour)
data.tail()
# # Let's start the analysis
# ## Analyze the Date of Month(DoM)
# In[15]:
hist(data.dom, bins=30, rwidth=.8 ,range=(0.5,30.5), color=('salmon'), alpha=(0.5))
xlabel('Date of the month')
ylabel('Frequency')
title('Uber - Frequency by Date of the month(DoM) April 2014')
# In[16]:
def count_rows(rows):
return len(rows)
by_date = data.groupby('dom').apply(count_rows)
by_date
# In[20]:
by_date_sorted = by_date.sort_values()
by_date_sorted
# In[89]:
bar(range(1,31),by_date_sorted)
seaborn.set(font_scale=0.7)
xticks(range(1,31), by_date_sorted.index)
xlabel('Day of the month')
ylabel('Frequency')
title('Uber - Frequency by date of month April 2014')
("")
# ## Analyze the Hour
# In[134]:
hist(data.hour,bins=24,range=(0.5,24),color=('indianred'), alpha=0.85)
xticks(range (0,24))
xlabel('hour of the day')
ylabel('Frequency')
title('Uber - Frequency by hour of the day April 2014')
("")
# ## Analyze the weekday
# In[102]:
hist(data.weekday,bins=7, range=(-.5,6.5), rwidth=.8, color='palevioletred', alpha=0.8)
xticks(range(7), 'Mon Tue Wed Thu Fri Sat Sun'.split())
xlabel('Weekday')
ylabel('Frequency')
title('Uber - Frequency by weekday April 2014')
("")
# ## Cross analysis (hour, day of the week)
# In[104]:
by_cross = data.groupby('weekday hour'.split()).apply(count_rows).unstack()
# In[106]:
seaborn.heatmap(by_cross)
("")
# ## Latitude and longitude analysis
# In[126]:
hist(data['Lat'], bins=100, range=(40.6,40.9), color='firebrick');
# In[125]:
hist(data['Lon'], bins=100, range=(-74.1,-73.9), color='sienna');
# In[136]:
hist(data['Lat'], bins=100, range=(40.6,40.9), color='darkorchid', alpha=0.5, label='latitude')
grid()
legend(loc='best')
twiny()
hist(data['Lon'], bins=100, range=(-74.1,-73.9), color='darkcyan', alpha=0.5, label='longitude')
grid()
legend(loc='upper left')
("")
# In[144]:
figure(figsize(20,20))
plot(data['Lon'], data['Lat'], '.', ms=1, alpha=.5)
xlim(-74.2,-73.7)
ylim(40.7,41)
# In[ ]: