-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex_pandas.py
605 lines (290 loc) · 9.31 KB
/
ex_pandas.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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
import ex_pandas as pd
import ex_numpy as np
import ex_matplotlib.pyplot as plt
#Series
pd.Series([5775, 373, 7, 42, np.nan, 33])
pd.Series(["cubs","royals","giants","sox","giants","cards","giants","...",None])
# Datetime index
dt_index = pd.date_range(start='2015-1-1', end='2015-11-1', freq='m')
dt_series = pd.Series(data= np.random.randn(10), index = dt_index)
dt_series
# Extracyt by date
dt_series[pd.to_datetime('2015-02-28')]
dt_series['2015-02-28']
#Indexing
series_1 = pd.Series(np.random.randn(5), index = ['California', 'Alabama', 'Indiana', 'Montana', 'Kentucky'])
series_2 = pd.Series(np.random.randn(5), index = ['Washington', 'Alabama', 'Montana', 'Indiana', 'New York'])
# add Series
series_1 + series_2
#DataFrames
pd.DataFrame({'a': [1,2], 'b': [2,5], 'c': [3,6]}, index=['foo', 'bar'])
pd.DataFrame(data=[[1, 2, 3], [4, 5, 6]],
columns=['a', 'b', 'c'],
index=['foo', 'bar']
)
df = pd.DataFrame(np.random.randn(10, 5), index=dt_index, columns=[x for x in 'abcde'])
df
# each dataframe column is a series:
col = df.a
type(col)
# so is each row.
row = df.loc['2015-01-31']
type(row)
# The columns all have the same index: The row names!
col.index
# What's the index for the rows? the column names!
row.index
# When one row is returned it is a Series (not a dataframe)
df.a
df['a']
# Return subset / multiple columns
df[['a','b']]
# Advanced Indexing
df.loc['2015-05-31':'2015-08-31', 'c':'e'] # Ranges by index label
df.iloc[2:-3, 2:5] # Ranges by index number.
#Fancy Indexing
dt_index = pd.date_range('2015-1-1', '2015-12-31', freq='m')
df = pd.DataFrame(np.random.randn(12,5), index=dt_index)
df.head()
# Adding new column
df['state'] = ['Alabama', 'Alaska' , 'Arizona'] * 4
df.head()
df = df.reset_index()
df.rename(columns={'index': 'date'}, inplace=True)
df = df.set_index(['state', 'date'])
df.head()
df.loc['Alabama'].head()
# this doesn't work because the date is not the primary (first) index
try:
df.loc['2015-01-31']
except KeyError as e:
print("ERROR: {}".format(e))
# but this does
df.loc[df.index.get_level_values('date') == '2015-01-31']
df.loc[('Alabama', '2015-01-31')] #or you can do this.
#I/O
df = pd.read_csv('data/winequality-red.csv', delimiter=';')
df.head() #Display the first x rows (default is 5)
df.shape
df.columns
df.info()
df.describe().round(3)
df.tail()
#Boolean Masks
# Pandas
#
#
# ## Pandas Series
# `pandas.series` are one dimensional np.ndarray vectors **with an index**
# In[24]:
pd.Series([5775, 373, 7, 42, np.nan, 33])
# In[25]:
pd.Series(["cubs","royals","giants","sox","giants","cards","giants","...",None])
# ## Datetime Index
# In[26]:
# Datetime index
dt_index = pd.date_range(start='2015-1-1', end='2015-11-1', freq='m')
dt_series = pd.Series(data= np.random.randn(10), index = dt_index)
dt_series
# In[27]:
dt_series[pd.to_datetime('2015-02-28')]
dt_series['2015-02-28']
# ## General Indexing
# - Pandas makes excellent use of informative indexes.
# - An index works like a dictionary key, enabling fast lookups of the data associated with the index.
# - Indexes also enable fast group-by, merge and time-series operations.
# - When you're really in the zone with pandas, you'll be thinking about indexes all the time.
# In[28]:
series_1 = pd.Series(np.random.randn(5), index = ['California', 'Alabama', 'Indiana', 'Montana', 'Kentucky'])
series_2 = pd.Series(np.random.randn(5), index = ['Washington', 'Alabama', 'Montana', 'Indiana', 'New York'])
# Pandas uses the index by default to align series for mathematical operations
# In[29]:
series_1 + series_2
# ## DataFrames
# `pandas.DataFrames` are set of `pandas.Series` that share the same index.
# There is more than one way to skin a cat.
# In[30]:
pd.DataFrame({'a': [1,2], 'b': [2,5], 'c': [3,6]}, index=['foo', 'bar'])
# In[31]:
pd.DataFrame(data=[[1, 2, 3], [4, 5, 6]],
columns=['a', 'b', 'c'],
index=['foo', 'bar']
)
# In[32]:
df = pd.DataFrame(np.random.randn(10, 5), index=dt_index, columns=[x for x in 'abcde'])
df
# In[33]:
# each dataframe column is a series:
col = df.a
type(col)
# In[34]:
# so is each row.
row = df.loc['2015-01-31']
type(row)
# In[35]:
# The columns all have the same index: The row names!
col.index
# In[36]:
# What's the index for the rows? the column names!
row.index
# ## Slice or View of a DataFrame
# In[37]:
# When one row is returned it is a Series (not a dataframe)
df.a
# In[38]:
df['a']
# In[39]:
# Return subset / multiple columns
df[['a','b']]
# ## Advanced Indexing
# `.loc` `.iloc` and `.xi`
# In[40]:
df.loc['2015-05-31':'2015-08-31', 'c':'e'] # Ranges by index label.
# In[41]:
df.iloc[2:-3, 2:5] # Ranges by index number.
# In[42]:
df.ix[2:-3,2:5] # Tries to estimate your request -- soon to be deprecated!
# In[43]:
df.ix['2015-05-31':'2015-08-31', 'c':'e']
# DO NOT USE `.ix`
# We show it because you may see it in legacy code, and should know what it is. But remember, in Python it is better to be explicit than implicit.
# --------------------------------------------------------------------------------------------
# ## Fancy Indexing
# In[44]:
dt_index = pd.date_range('2015-1-1', '2015-12-31', freq='m')
df = pd.DataFrame(np.random.randn(12,5), index=dt_index)
df.head()
# In[45]:
# Adding new column
df['state'] = ['Alabama', 'Alaska' , 'Arizona'] * 4
df.head()
# In[46]:
df = df.reset_index()
df.rename(columns={'index': 'date'}, inplace=True)
df = df.set_index(['state', 'date'])
df.head()
# In[47]:
df.loc['Alabama'].head()
# In[48]:
# this doesn't work because the date is not the primary (first) index
try:
df.loc['2015-01-31']
except KeyError as e:
print("ERROR: {}".format(e))
# In[49]:
# but this does
df.loc[df.index.get_level_values('date') == '2015-01-31']
# In[50]:
df.loc[('Alabama', '2015-01-31')] #or you can do this.
# ## IO
# In[51]:
df = pd.read_csv('data/winequality-red.csv', delimiter=';')
# In[52]:
df.head() #Display the first x rows (default is 5)
# In[53]:
df.shape
# In[54]:
df.columns
# In[55]:
df.info()
# In[56]:
df.describe().round(3)
# In[57]:
df.tail()
# ## Boolean Masks
# In[58]:
boolean_mask = df['chlorides'] <= df.chlorides.quantile(0.01)
# In[59]:
df[boolean_mask].shape
# In[60]:
# we use masks all the time in pandas (and numpy for that matter)
df[df.chlorides <= df.chlorides.quantile(0.01)]
# In[61]:
# we can easily extend this to more conditionals in our mask
df[(df['chlorides'] >= 0.04) & (df['chlorides'] < 0.08)].head()
# In[62]:
df.groupby('quality') # Note that this returns back to us a groupby object. It doesn't actually
# return to us anything useful until we perform some aggregation on it.
# In[63]:
# we can also group by multilple columns by passing them in in a list.
# pandas will group by the second column within the groups created by the first groupby.
df.groupby(['pH', 'quality']).mean()['chlorides']
# ## Add / Remove Columns
# In[64]:
# compute a new feature from the data
df['pct_free_sulf'] = df['free sulfur dioxide'] / df['total sulfur dioxide']
# In[65]:
df.head()
# In[66]:
# Drop a row
df.drop('pct_free_sulf', axis=1).head()
# In[67]:
# looks like the column is gone! let's check
df.columns
# In[68]:
# huh??? The operation is not inplace by default.
df.drop('pct_free_sulf', axis=1, inplace=True)
print(df.columns)
# ## Handling Missing Values
# * http://pandas.pydata.org/pandas-docs/stable/missing_data.html
# In[69]:
miss_val_df = pd.DataFrame(
[[1, 2, 3], [4, np.nan, 6]],
columns=['a', 'b', 'c'],
index=['foo', 'bar'])
miss_val_df
# When we come across a missing value, we can choose to impute or drop the missing value, or do nothing.
# In[70]:
miss_val_df.fillna(0)
# In[71]:
miss_val_df.dropna(how='any')
# In[72]:
miss_val_df.dropna(how='all')
# ## Merge
#
# Pandas supports SQL-like joins. left, right, outer, and inner joins all work as you would expect.
# Here are lots of examples: http://pandas.pydata.org/pandas-docs/stable/merging.html
# In[73]:
df1 = pd.DataFrame(
[[1, 2, 3], [4, 5, 6]],
columns=['a', 'b', 'c'])
df2 = pd.DataFrame(
[[26, 2, 25], [52, 5, 50]],
columns=['z', 'b', 'y'])
print(df1)
print(df2)
# In[74]:
df1.merge(df2, how='inner')
# In[75]:
df3 = pd.DataFrame(
[[1, 2, 3], [4, 5, 6], [7, 8, 9]],
columns=['a', 'b', 'c'])
# In[76]:
df1.merge(df2).merge(df3, how='inner')
# In[77]:
df1.merge(df2).merge(df3, how='outer')
# ## Concatenate
# * adding *rows*
# * see also: df.append()
# In[78]:
df1 = pd.DataFrame(
{'Col1': range(5), 'Col2': range(5), 'Col3': range(5)})
df2 = pd.DataFrame(
{'Col1': range(5), 'Col2': range(5), 'Col4': range(5)},
index=range(5, 10))
# In[79]:
df1
# In[80]:
df2
# In[81]:
# concatenate along the row index
pd.concat([df1, df2], axis=0)
# In[82]:
# concatenate along the column index
pd.concat([df1, df2], join='outer', axis=1)
# ## Conclusion
# - There a literally a billion things you can do in Pandas.
# - It is a powerful tool for exploratory data analysis, visualization and organization.
# - It is often my go-to when I start on a new dataset or a new problem.
# - Here is one of my favorite all-time pandas resources: [useful pandas snippets](https://gist.github.com/bsweger/e5817488d161f37dcbd2)
# - Now go forth and explore!