-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlesson5.py
293 lines (181 loc) · 6.17 KB
/
lesson5.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 31 16:57:46 2018
@author: lpa2a
"""
from pandas import Series, DataFrame
import pandas as pd
import numpy as np
from numpy import nan as NA
### Descriptive statistics
df = DataFrame([[1.4, np.nan], [7.1, -4.5],
[np.nan, np.nan], [0.75, -1.3]],
index=['a', 'b', 'c', 'd'],
columns=['one', 'two'])
df
df.sum()
df.sum(axis=1) # NB for this one NaNs are treated at 0
df.cumsum()
df.mean(axis=1, skipna=False)
df.describe() # also works on other objects
df.idxmax() # returns the id of the index of the max
### Handling Missing Data
string_data = Series(['aardvark', 'artichoke', np.nan, 'avocado'])
string_data
string_data.isnull()
string_data[0]=None
string_data.isnull()
data = Series([1, NA, 3.5, NA, 7])
data.dropna()
data[data.notnull()] # another way to do it
data = DataFrame([[1., 6.5, 3.], [1., NA, NA],
[NA, NA, NA], [NA, 6.5, 3.]])
cleaned = data.dropna() # row wise
data
cleaned
data.dropna(how='all') # only if whole row is NA
data[4] = NA
data
data.dropna(axis=1, how='all') # for columns
df = DataFrame(np.random.randn(7, 3))
df.iloc[:4, 1] = NA; df.iloc[:2, 2] = NA # nb book uses ix
df
df.dropna(thresh=3)
df.dropna(thresh=2)
df.dropna(thresh=1)
df.fillna(0)
df.fillna({1: 0.5, 3: -1})
df.fillna({1: 0.5, 2: -1})
df.fillna(0, inplace=True)
df
df = DataFrame(np.random.randn(6, 3))
df.iloc[2:, 1] = NA; df.iloc[4:, 2] = NA
df
df.fillna(method='ffill')
df.fillna(method='ffill', limit=2)
data = Series([1., NA, 3.5, NA, 7])
data.fillna(data.mean())
### Hierarchical Indexing -- adding higher dimensionality -- no examples
### read_csv et al
df = pd.read_csv('ex1.csv')
df
pd.read_table('ex1.csv', sep=',')
pd.read_csv('ex2.csv', header=None)
pd.read_csv('ex2.csv', names=['a', 'b', 'c', 'd', 'message'])
names = ['a', 'b', 'c', 'd', 'message']
pd.read_csv('ex2.csv', names=names, index_col='message')
result = pd.read_table('ex3.txt', sep='\s+')
result
pd.read_csv('ex4.csv', skiprows=[])
pd.read_csv('ex4.csv', skiprows=[0, 2, 3])
result = pd.read_csv('ex5.csv')
result
### NB the value NA became NaN -- this is called a sentinel
### NB the empty value is also coded as NaN
sentinels = {'message': ['foo', 'NA'], 'something': ['two']}
pd.read_csv('ex5.csv', na_values=sentinels)
### reading text files in pieces
result = pd.read_csv('ex6.csv')
result
pd.read_csv('ex6.csv', nrows=5)
chunker = pd.read_csv('ex6.csv', chunksize=10)
chunker
tot = Series([])
for chunk in chunker:
tot = tot.add(chunk['key'].value_counts(), fill_value=0)
tot
### Writing out data -- just like read in no examples
### stop for today
### JSON
obj = """
{"name": "Wes",
"places_lived": ["United States", "Spain", "Germany"],
"pet": null,
"siblings": [{"name": "Scott", "age": 25, "pet": "Zuko"},
{"name": "Katie", "age": 33, "pet": "Cisco"}]
}
"""
import json
result = json.loads(obj) # To convert a JSON string to Python form, use json.loads
result
type(result)
#json.dumps on the other hand converts a Python object back to JSON:
asjson = json.dumps(result)
# JSON --> Python use json.loads
# Python --> JSON use json.dumps
# for more see example in chapter 7 about USDA
### Web APIs
import requests
url = 'https://api.github.com/repos/pydata/pandas/milestones/28/labels'
resp = requests.get(url)
resp
# status cat - https://http.cat/
data = resp.json() # new way of handling JSON built in to requests
data[:5]
issue_labels = DataFrame(data)
issue_labels
### Database Operations
## Throw out everything you know about databases
df1 = DataFrame({'key': ['b', 'b', 'a', 'c', 'a', 'a', 'b'],
'data1': range(7)})
df2 = DataFrame({'key': ['a', 'b', 'd'],
'data2': range(3)})
df1
df2
pd.merge(df1,df2) # nb we use pd.merge not df1.merge
### nb no key specified it defaulted to similiar key
pd.merge(df1,df2,on='key')
df3 = DataFrame({'lkey': ['b', 'b', 'a', 'c', 'a', 'a', 'b'],
'data1': range(7)})
df4 = DataFrame({'rkey': ['a', 'b', 'd'],
'data2': range(3)})
pd.merge(df3, df4, left_on='lkey', right_on='rkey')
### NB: By default merge does an 'inner' join
pd.merge(df1, df2, how='outer',on='key')
df1 = DataFrame({'key': ['b', 'b', 'a', 'c', 'a', 'b'],
'data1': range(6)})
df2 = DataFrame({'key': ['a', 'b', 'a', 'b', 'd'],
'data2': range(5)})
df1
df2
pd.merge(df1, df2, on='key', how='left')
# many-to-many merge give use cartesian product
# eg: a gets 4 entries because 2 in each df
# multi key merge
left = DataFrame({'key1': ['foo', 'foo', 'bar'],
'key2': ['one', 'two', 'one'],
'lval': [1, 2, 3]})
right = DataFrame({'key1': ['foo', 'foo', 'bar', 'bar'],
'key2': ['one', 'one', 'one', 'two'],
'rval': [4, 5, 6, 7]})
pd.merge(left, right, on=['key1', 'key2'], how='outer')
pd.merge(left, right, on='key1')
# pandas added _x and _y because the key was repeated
# you can set the suffixes with the argument
pd.merge(left, right, on='key1', suffixes=('_left', '_right'))
### Now let's use Index for merging
left1 = DataFrame({'key': ['a', 'b', 'a', 'a', 'b', 'c'],
'value': range(6)})
right1 = DataFrame({'group_val': [3.5, 7]}, index=['a', 'b'])
left1
right1
pd.merge(left1, right1, left_on='key', right_index=True)
# hierarchical indexing is complicated best for the privacy of your own office
# now really forget everything you knew about databases
left2 = DataFrame([[1., 2.], [3., 4.], [5., 6.]], index=['a', 'c', 'e'],
columns=['Ohio', 'Nevada'])
right2 = DataFrame([[7., 8.], [9., 10.], [11., 12.], [13, 14]],
index=['b', 'c', 'd', 'e'], columns=['Missouri', 'Alabama'])
left2
right2
left2.join(right2, how='outer') # yep, now we use join and its a member of the dataframe
# It also supports joining the index of the passed DataFrame on one of the columns of the calling DataFrame:
left1.join(right1, on='key')
# uses 'key' from left1 and index from right1
### concatenate
### combining data with overlap
### pivoting
### removing duplicates
### transforming data using a function or mapping
###