-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadvanced_dataframes.py
More file actions
294 lines (137 loc) · 5.16 KB
/
Copy pathadvanced_dataframes.py
File metadata and controls
294 lines (137 loc) · 5.16 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
#!/usr/bin/env python
# coding: utf-8
# In[7]:
import pandas as pd
import numpy as np
from pydataset import data
from env import username, hostname, password
def get_db_url(username, hostname, password, database):
url = f'mysql+pymysql://{username}:{password}@{hostname}/{database}'
return url
# In[8]:
url = get_db_url(username, hostname, password, 'employees')
# In[11]:
employees = pd.read_sql('SELECT * FROM employees', url)
employees
# In[12]:
titles = pd.read_sql('SELECT * FROM titles', url)
titles
# In[35]:
employees.info(), employees.describe()
# In[36]:
titles.info(), titles.describe()
# In[25]:
titles.title.nunique()
# In[39]:
titles.to_date.max()
# In[26]:
titles.to_date.min()
# In[29]:
from datetime import date
titles[titles.to_date < date.today()].sort_values(by='to_date', ascending=False).head(1)
# In[54]:
# Exercises II
# In[31]:
# 1. Copy the users and roles DataFrames from the examples above.
# Create the users DataFrame.
users = pd.DataFrame({
'id': [1, 2, 3, 4, 5, 6],
'name': ['bob', 'joe', 'sally', 'adam', 'jane', 'mike'],
'role_id': [1, 2, 3, 3, np.nan, np.nan]
})
users
# In[32]:
# Create the roles DataFrame
roles = pd.DataFrame({
'id': [1, 2, 3, 4],
'name': ['admin', 'author', 'reviewer', 'commenter']
})
roles
# In[33]:
# 2. What is the result of using a right join on the DataFrames?
users.merge(roles, how='right', left_on='role_id', right_on='id')
# In[35]:
# 3. What is the result of using an outer join on the DataFrames?
users.merge(roles, how='outer', left_on='role_id', right_on='id')
# In[37]:
# 4. What happens if you drop the foreign keys from the DataFrames and try to merge them?
users_dropped = users.drop(columns='role_id')
roles_dropped = roles.drop(columns='id')
users_dropped.merge(roles_dropped, how='outer')
# In[40]:
# 5. Load the mpg dataset from PyDataset.
mpg = data('mpg')
mpg
# In[36]:
# 6. Output and read the documentation for the mpg dataset.
data('mpg', show_doc=True)
# In[41]:
# 7. How many rows and columns are in the dataset?
mpg.shape
# In[47]:
# 8. Check out your column names and perform any cleanup you may want on them.
mpg = mpg.rename(columns={'cty':'city', 'hwy':'highway'})
mpg
# In[99]:
# 9. Display the summary statistics for the dataset.
mpg.info(), mpg.describe()
# In[43]:
# 10. How many different manufacturers are there?
mpg['manufacturer'].nunique()
# In[45]:
# 11. How many different models are there?
mpg['model'].nunique()
# In[60]:
# 12. Create a column named mileage_difference like you did in the DataFrames exercises; this column should contain the difference between highway and city mileage for each car.
mpg['mileage_diff'] = mpg['highway'] - mpg['city']
mpg
# In[49]:
# 13. Create a column named average_mileage like you did in the DataFrames exercises; this is the mean of the city and highway mileage.
mpg = mpg.assign(avg_mileage = round(((mpg['highway']+mpg['city'])/2), 2))
mpg
# In[50]:
# 14. Create a new column on the mpg dataset named is_automatic that holds boolean values denoting whether the car has an automatic transmission.
mpg = mpg.assign(is_auto = mpg['trans'].str.contains('auto'))
mpg
# In[73]:
# 15. Using the mpg dataset, find out which which manufacturer has the best miles per gallon on average?
mpg.groupby('manufacturer')['avg_mileage'].mean().sort_values(ascending=False)
# In[72]:
# 16. Do automatic or manual cars have better miles per gallon?
mpg.groupby('is_auto')['avg_mileage'].mean().rename(index={0:'Manual', 1:'Automatic'})
# In[81]:
# 1. Use your get_db_url function to help you explore the data from the chipotle database.
chipotle_url = get_db_url(username, hostname, password, 'chipotle')
chipotle = pd.read_sql('SELECT * FROM orders', chipotle_url)
chipotle
# In[82]:
# 2. What is the total price for each order?
chipotle['item_price'] = chipotle['item_price'].str.replace('$','').astype('float')
chipotle
# In[84]:
chipotle.groupby('order_id')['item_price'].sum()
# In[85]:
# 3. What are the most popular 3 items?
chipotle.groupby('item_name')['quantity'].sum().nlargest(3)
# In[86]:
# 4. Which item has produced the most revenue?
chipotle.groupby('item_name')['item_price'].sum().nlargest(1)
# In[90]:
# 5. Join the employees and titles DataFrames together.
emp_title = employees.merge(titles, on='emp_no')
emp_title
# In[91]:
# 6. For each title, find the hire date of the employee that was hired most recently with that title.
emp_title.groupby('title')['hire_date'].max()
# In[93]:
# 7. Write the code necessary to create a cross tabulation of the number of titles by department.
## (Hint: this will involve a combination of SQL code to pull the necessary
### data and python/pandas code to perform the manipulations.)
emp_titles = pd.read_sql('SELECT * FROM employees JOIN titles USING(emp_no) WHERE to_date > CURDATE()', url)
dept_emp = pd.read_sql('SELECT * FROM dept_emp', url)
departments = pd.read_sql('SELECT * FROM departments', url)
# In[95]:
emp_title_dept = emp_titles.merge(dept_emp, how='inner', on='emp_no')
full_db = emp_title_dept.merge(departments, how='inner', on='dept_no')
pd.crosstab(full_db.title, full_db.dept_name).T
# In[ ]: