-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathdatawrangling.py
558 lines (372 loc) · 17.1 KB
/
datawrangling.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
'''
=================================
Pandas - Data Wrangling in python
=================================
If you ever work as a fancy Data Scientists, it is very likely that 80% of your
time will be spent doing data wrangling (I prefer the term Data Cooking).
This will continue until you finally achieve a managerial position and stop
doing useful work, all of a sudden.
Input data --> Data Cooking ---> Visualization
|
Intermediate Files
ModelOps[https://en.wikipedia.org/wiki/ModelOps]
------>
MLOps
------>
DevOps
------>
DevSecOps
------>
Software development, in any form, is a human endeavor: it requires to articulate
people working together in teams.
TDSP: Team Data Science Process
Input Data:
⏺ Data Sources
Data Cooking:
⏺ Dealing with missing data.
⏺ ETL: extract transform and load. DTS: Data tranformation services
Visualization
⏺ Basic Visualization
This script contains several snippets, separtaed by '# %%' which is a special
marker that allows Visual Studio Code to treat a python script as a jupyter
console notebook.
References:
- Wes McKinney, Python for Data Analysis, 2017
- Harrison, Learning the Pandas Library, 2016
'''
# %% -----------------------------------------------------------------------------
print(__doc__)
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import requests
from io import StringIO
# %% -----------------------------------------------------------------------------
print('The whole picture, working in Data Science Project in corporate environments')
# https://docs.microsoft.com/en-us/azure/machine-learning/team-data-science-process/overview
# %% -----------------------------------------------------------------------------
print('Hello Python Scientific World')
online = True
if (online == True):
url = requests.get('https://raw.githubusercontent.com/faturita/python-scientific/master/data/blinking.dat')
csv_raw = StringIO(url.text)
signals = pd.read_csv(csv_raw, delimiter=' ', names = ['timestamp','counter','eeg','attention','meditation','blinking'])
else:
signals = pd.read_csv('data/blinking.dat', delimiter=' ', names = ['timestamp','counter','eeg','attention','meditation','blinking'])
print('Signals is a dataframe, the basic pandas structure.')
print('Information:')
print(signals.head())
print('Filter records:')
print(signals[signals.counter > 45])
# Show a resume of the data contained in data
signals.describe()
# %% -----------------------------------------------------------------------------
print('You can always extract the numpy tensor from the pandas dataframe.')
data = signals.values
print('Now in "data", you have a tensor.')
print (data)
print('Shape %2d,%2d:' % (signals.shape))
print('From here you can start working around the data structure which has a mathematical purpose.')
# %% -----------------------------------------------------------------------------
print('You can go the other way around and convert a numpy array into a dataframe.')
print('Dataframe contain metadata information.')
databack = pd.DataFrame(data, columns=['ts', 'ct', 'e','att','med','blk'])
print ('Shape %2d,%2d:' % databack.shape)
# %% -----------------------------------------------------------------------------
print('Visualizations')
import seaborn as sns
sns.set(style="darkgrid")
sns.lineplot(x="timestamp", y="eeg", hue="attention", data=signals)
import matplotlib.pyplot as plt
plt.show()
# %% -----------------------------------------------------------------------------
print('Pandas is great to read data from several differentes sources.')
print('Create a Dataframe from a python dictionary.')
jsonlike = {
'cars': ["BMW", "Volvo", "Ford"],
'passings': [3, 7, 2]
}
mydataframe = pd.DataFrame(jsonlike)
print(mydataframe)
print('The columns names are taken from the dictionary while row names are just consequitive numbers.')
# %% -----------------------------------------------------------------------------
print('The read_xxxx methods load data in several formats. This like Excel import, so there are hundreds of parameters.')
signals = pd.read_csv('data/blinking.dat', delimiter=' ', names = ['timestamp','counter','eeg','attention','meditation','blinking'])
# //skip_rows, index_cols, sep='\s+', nrows
print(signals.head())
print('read_sas, read_sql, read_pickle, read_json, read_excel, ...')
dat = pd.read_csv('data/laliga.csv',delimiter=';')
print(dat.head())
# %% -----------------------------------------------------------------------------
print('Data can finally be exported to an output file.')
dat.to_csv('data/out.csv')
# %% -----------------------------------------------------------------------------
print('JSON is a widespread format very used in web environments.')
obj = """
{"name": "Wes",
"places_lived": ["United States", "Spain", "Germany"],
"pet": null,
"siblings": [{"name": "Scott", "age": 30, "pets": ["Zeus", "Zuko"]},
{"name": "Katie", "age": 38,
"pets": ["Sixes", "Stache", "Cisco"]}]
} """
import json
results = json.loads(obj)
results
print('We can filter what part of the JSON object do we want for the Dataframe.')
siblings = pd.DataFrame(results['siblings'], columns=['name', 'age'])
print(siblings)
json_string = json.dumps(results)
print(json_string)
# %% -----------------------------------------------------------------------------
data = pd.read_json('data/sample2.json')
print('It is possible to put it back to json string.')
print(data.to_json())
# %% -----------------------------------------------------------------------------
print('Python allows to crawl web pages and get HTML tables.')
tables = pd.read_html('https://www.scimagojr.com/journalrank.php') # @NOTE Fixed
journals = tables[0]
print("Journal rankings by SciMago")
print(journals)
# %% -----------------------------------------------------------------------------
print('Series are one-index Dataframe.')
calories = {"day1": 420, "day2": 380, "day3": 390}
cal_frame = pd.Series(calories)
print(cal_frame)
print('Day1, day2, ... are the row names.')
# %% -----------------------------------------------------------------------------
print('Python uses pickles to serialize structures, and this can be read from pandas.')
calories = {"day1": 420, "day2": 380, "day3": 390}
cal_frame = pd.Series(calories)
cal_frame.to_pickle('data/frame.pickle')
# Store intermediate information
new_frame = pd.read_pickle('data/frame.pickle')
print(new_frame)
# %% -----------------------------------------------------------------------------
print('Reading some json data out of a API.')
from io import StringIO
import requests
url = requests.get('https://api.github.com/repos/faturita/python-scientific/commits')
js = StringIO(url.text)
sg = pd.read_json(js)
sg.head()
# %% -----------------------------------------------------------------------------
print('-------------------------------------------------------------------------------------')
print('Panda detects missing data by checking sentinel values like "NaN", "NULL", "NA".')
results = pd.read_csv('data/sample1.csv')
print('You can get an indicative matrix which returns true on the cell where the data is missing')
# Isnull and notnull return a boolean Panda Frame
results.isnull()
results.notnull()
print('It is also possible to specify different sentinel values for missing data.')
sentinels = {'message': ['foo', 'NA'], 'something': ['two']}
print('For each column, the sentinel value.')
results = pd.read_csv('data/sample1.csv', na_values = sentinels)
print ( results )
print('Filter elements from the DataFrame where the specified column is NA.')
results[results.notnull()['message']]
# %% -----------------------------------------------------------------------------
print('Handling missing data.')
from numpy import nan as NA
data = pd.Series([1, NA, 3.5, NA, 7])
newdataframe = data.dropna()
print(newdataframe)
print('The same as')
print(data[data.notnull()])
# %% -----------------------------------------------------------------------------
print('On 2D')
data = pd.DataFrame([[1., 6.5, 3.], [1., NA, NA], [NA, NA, NA], [NA, 6.5, 3.]])
data.dropna(how='all', axis = 1) # 0 is row, 1 is col
data.dropna(how='any', axis = 0)
# %% -----------------------------------------------------------------------------
print('Now fillna can be used in the same way to fill in the data.')
data = pd.DataFrame([[1., 6.5, 3.], [1., NA, NA], [NA, NA, NA], [NA, 6.5, 3.]])
newframe = data.fillna(0)
data.fillna({0: 0.5, 2:0}) # When NaN is found, for the row 0 put 0.5, for the row 2, put 0
print('An interpolation can be performed to fill in the missing values.')
data.fillna(method='ffill')
data[np.abs(data) > 1.5] = np.sign(data) * 10
# %% -----------------------------------------------------------------------------
print('None, the python Null marker, can also be considered as NA sentinel.')
string_data = pd.Series(['aardvark', 'artichoke', np.nan, 'avocado'])
string_data[0] = None
print(string_data)
# %% -----------------------------------------------------------------------------
print('Removing duplicates')
string_data = pd.Series(['Newark', 'Manchester', 'Halifax', 'Manchester'])
newframe = string_data.drop_duplicates()
print(string_data)
print(newframe)
# %% ----------------------------------------------------------------------------
print('Replacing values')
newframe = string_data.replace({'Newark': np.nan, 'Halifax': 'Ottawa'})
print(string_data)
print(newframe)
# %% -----------------------------------------------------------------------------
print('Pandas Series can be used to handle sequential data.')
import pandas as pd
a = [1, 7, 2]
series = pd.Series(a)
print(series)
print('By default, indexes are created with range on the number of elements.')
series = pd.Series(a, index = ["x", "y", "z"])
print(series)
print(series.values)
print(series.index)
# %% -----------------------------------------------------------------------------
print('String indexes can be used on series.')
a = [1, 7, 2]
series = pd.Series(a, index = ["x", "y", "z"])
print(series['x'])
print(series[series > 1])
print(series * 2)
print(np.exp(series))
print('x' in series)
print('r' in series)
# %% -----------------------------------------------------------------------------
print('Index are automatically used to operate on the data.')
a = [1, 7, 2]
series1 = pd.Series(a, index = ["x", "y", "z"])
b = [-1, -8, 3]
series2 = pd.Series(a, index = ["y", "z", "r"])
# WARNING !
series3 = series1 + series2
print(series1)
print(series2)
print(series3)
# %% -----------------------------------------------------------------------------
print('Dataframes are dynamics structures')
data = {'state': ['Ohio', 'Ohio', 'Ohio', 'Nevada', 'Nevada', 'Nevada'],
'year': [2000, 2001, 2002, 2001, 2002, 2003],
'pop': [1.5, 1.7, 3.6, 2.4, 2.9, 3.2]}
frame = pd.DataFrame(data)
print(frame)
print(frame.state)
print(frame['state'])
print(frame.pop)
# %% -----------------------------------------------------------------------------
print('Row can also have names (instead of index numbers)')
data = {'state': ['Ohio', 'Ohio', 'Ohio', 'Nevada', 'Nevada', 'Nevada'],
'year': [2000, 2001, 2002, 2001, 2002, 2003],
'pop': [1.5, 1.7, 3.6, 2.4, 2.9, 3.2]}
frame = pd.DataFrame(data)
print(frame)
frame.index = ['one','two','three','four','five','six']
print(frame)
# %% -----------------------------------------------------------------------------
print('Entire rows or columns can be updated')
data = {'state': ['Ohio', 'Ohio', 'Ohio', 'Nevada', 'Nevada', 'Nevada'],
'year': [2000, 2001, 2002, 2001, 2002, 2003],
'pop': [1.5, 1.7, 3.6, 2.4, 2.9, 3.2]}
frame = pd.DataFrame(data)
print(frame)
frame['pop'] = 9.0 # References the column 'pop'
frame.loc['three']=9.0 # References the row indexed 'three'
print(frame)
# %% -----------------------------------------------------------------------------
print('Colums/Rows can be updated or easily created')
data = {'state': ['Ohio', 'Ohio', 'Ohio', 'Nevada', 'Nevada', 'Nevada'],
'year': [2000, 2001, 2002, 2001, 2002, 2003],
'pop': [1.5, 1.7, 3.6, 2.4, 2.9, 3.2]}
frame = pd.DataFrame(data)
print(frame)
val = pd.Series([11, 12, 13], index=['two', 'four', 'five'])
frame['pop'] = val # This means that pop is REPLACED by val.
print('Now pop has been replaced with a new series')
print(frame)
print('Now add a new column')
frame['Casinos'] = frame.state == 'Nevada'
print(frame)
del frame['Casinos'] # Removes the column 'Casinos'
print('And delete it later')
print(frame)
frame.columns.name = 'Variables'
frame.index.name = 'Locations'
print('Now we can add index names (name for the column and the row)')
print(frame)
# %% -----------------------------------------------------------------------------
print('Reindexing: mapping the values with new indexes.')
a = [1, 7, 2]
series1 = pd.Series(a, index = ["x", "y", "z"])
series2 = series1.reindex(["x","z","e"])
series2.index = ['x','p','d'] # Is the same result ?
# %% -----------------------------------------------------------------------------
data = pd.DataFrame(np.arange(16).reshape((4, 4)),
index=['Ohio', 'Colorado', 'Utah', 'New York'],
columns=['one', 'two', 'three', 'four'])
data.drop(['Colorado', 'Ohio'])
data.drop('four', axis=1) # inplace=True, mutate the object.
print(data)
# %% -----------------------------------------------------------------------------
print('Slicing, the end-point is inclusive')
data = pd.DataFrame(np.arange(16).reshape((4, 4)),
index=['Ohio', 'Colorado', 'Utah', 'New York'],
columns=['one', 'two', 'three', 'four'])
data['Ohio':'Utah'] # Filters from Ohio to Utah inclusive.
data.loc['Colorado', ['two', 'three']]
data.iloc[2, [3, 0, 1]]
data.loc[:'Utah', 'two']
data.iloc[:, :3][data.three > 5]
# %% -----------------------------------------------------------------------------
print('Mappings')
frame = pd.DataFrame(np.random.randn(4,3), columns=list('bde'),
index=['Utah','Ohio','Texas','Oregon'])
np.abs(frame)
f = lambda x: x.max() - x.min()
# Mappings can be applied directly on dataframes.
frame.apply(f)
# Or they can also be applied specifically to certain columns or rows.
frame.apply(f, axis=1)
frame.apply(f, axis='columns')
# %% -----------------------------------------------------------------------------
frame = pd.DataFrame(np.random.randn(4,3), columns=list('bde'),
index=['Utah','Ohio','Texas','Oregon'])
format = lambda x: '%.2f' % x
print(frame.applymap(format))
print(frame['e'].map(format))
# %% -----------------------------------------------------------------------------
print('Index uniqueness')
obj = pd.Series(range(5), index=['a', 'a', 'b', 'b', 'c'])
obj.index.is_unique
obj['a'] # This is now a Series
obj['c'] # This is a scalar value
# %% -----------------------------------------------------------------------------
print('Aggregating by hierarchical indexing.')
data = pd.Series(np.random.randn(9),
index=[['a', 'a', 'a', 'b', 'b', 'c', 'c', 'd', 'd'],
[1,2,3,1,3,1,2,2,3]])
data['b']
data['b':'c']
data.unstack() # Set the doble index as a matrix.
data.unstack().stack() # Put it back into hierarchical
# %% -----------------------------------------------------------------------------
print('Aggregating indexes.')
frame = pd.DataFrame({'a': range(7), 'b': range(7,0,-1),
'c': ['one','one','one','two','two','two','two'],
'd': [0,1,2,0,1,2,3]})
frame.set_index(['c','d'], drop=False) # 'c' and 'd' are now indexes. Should we keep the columns or not.
frame.reset_index()
# %% -----------------------------------------------------------------------------
print('Aggregating by two columnds.')
data = [{'Year':2000, 'Yields': np.random.randint(60), 'Location':'Colchester'},
{'Year':2000, 'Yields': np.random.randint(60), 'Location':'Manchester'},
{'Year':2001, 'Yields': np.random.randint(60), 'Location':'Colchester'},
{'Year':2001, 'Yields': np.random.randint(60), 'Location':'Oxford'},
{'Year':2002, 'Yields': np.random.randint(60), 'Location':'Colchester'},
{'Year':2002, 'Yields': np.random.randint(60), 'Location':'Oxford'},
{'Year':2003, 'Yields': np.random.randint(60), 'Location':'Manchester'}]
df = pd.DataFrame(data, columns=['Year', 'Yields', 'Location'])
grp = df.groupby(['Location','Year']).agg({'Yields':['mean','min','max']})
grp.columns = ['yield_mean','yield_min','yield_max']
grp = grp.reset_index()
print(grp)
# %% -----------------------------------------------------------------------------
print('Data frames can be easily be used for basic plotting.')
df = pd.read_csv('data/laliga.csv',delimiter=';')
df.plot(kind='scatter', x = 'MP', y = 'FA_GIVEN')
plt.show()
# %% -----------------------------------------------------------------------------
print('Data frames can be easily be used for basic plotting.')
df['GOAL'].plot(kind = 'hist')
plt.show()
# %%