-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamples.py
285 lines (247 loc) · 7.66 KB
/
examples.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
import pandas as pd
from pandas import json_normalize
from tabulate import tabulate
from flatten_json import flatten, flatten_json
from flattern import NotADictionary
now = pd.datetime.datetime.now()
print(now
)
def flatten(obj: dict) -> dict:
"""
This function takes a dictionary with arbitrary levels of nested
lists and dictionaries and flattens it.
Raises NotADictionary if the input is invalid.
"""
if not isinstance(obj, dict):
raise NotADictionary
flattened_data = {}
def flatten_json(json_data, name=''):
if type(json_data) is dict:
for key in json_data:
if not bool(json_data[key]): # handle empty dict values
flattened_data[name + key + '.'[:-1]] = json_data[key]
else:
flatten_json(json_data[key], name + key + '.')
elif type(json_data) is list:
i = 0
for key in json_data:
flatten_json(key, name + str(i) + '.')
i += 1
else:
flattened_data[name[:-1]] = json_data
try:
flatten_json(obj)
except Exception as e:
raise e
return flattened_data
json_obj = {
"executionPlan": {
"_id": "fdd38b4a-493f-5b9e-b462-28c07ac7fc07",
"name": "Databricks Shell",
"systemInfo": {
"name": "spark",
"version": "3.3.0"
},
"agentInfo": {
"name": "spline",
"version": "0.7.12"
},
"extra": {
"appName": "Databricks Shell",
"attributes": [
{
"id": "fdd38b4a-493f-5b9e-b462-28c07ac7fc07:attr-0",
"name": "circuitId",
"dataTypeId": "e63adadc-648a-56a0-9424-3289858cf0bb"
},
{
"id": "fdd38b4a-493f-5b9e-b462-28c07ac7fc07:attr-1",
"name": "circuitRef",
"dataTypeId": "75fe27b9-9a00-5c7d-966f-33ba32333133"
},
{
"id": "fdd38b4a-493f-5b9e-b462-28c07ac7fc07:attr-2",
"name": "name",
"dataTypeId": "75fe27b9-9a00-5c7d-966f-33ba32333133"
},
{
"id": "fdd38b4a-493f-5b9e-b462-28c07ac7fc07:attr-3",
"name": "location",
"dataTypeId": "75fe27b9-9a00-5c7d-966f-33ba32333133"
},
{
"id": "fdd38b4a-493f-5b9e-b462-28c07ac7fc07:attr-4",
"name": "country",
"dataTypeId": "75fe27b9-9a00-5c7d-966f-33ba32333133"
},
{
"id": "fdd38b4a-493f-5b9e-b462-28c07ac7fc07:attr-5",
"name": "lat",
"dataTypeId": "a155e715-56ab-59c4-a94b-ed1851a6984a"
},
{
"id": "fdd38b4a-493f-5b9e-b462-28c07ac7fc07:attr-6",
"name": "lng",
"dataTypeId": "a155e715-56ab-59c4-a94b-ed1851a6984a"
},
{
"id": "fdd38b4a-493f-5b9e-b462-28c07ac7fc07:attr-7",
"name": "alt",
"dataTypeId": "e63adadc-648a-56a0-9424-3289858cf0bb"
},
{
"id": "fdd38b4a-493f-5b9e-b462-28c07ac7fc07:attr-8",
"name": "url",
"dataTypeId": "75fe27b9-9a00-5c7d-966f-33ba32333133"
}
],
"dataTypes": [
{
"_typeHint": "dt.Simple",
"id": "e63adadc-648a-56a0-9424-3289858cf0bb",
"name": "int",
"nullable": "true"
},
{
"_typeHint": "dt.Simple",
"id": "75fe27b9-9a00-5c7d-966f-33ba32333133",
"name": "string",
"nullable": "true"
},
{
"_typeHint": "dt.Simple",
"id": "a155e715-56ab-59c4-a94b-ed1851a6984a",
"name": "double",
"nullable": "true"
}
]
},
"inputs": [
{
"sourceType": "csv",
"source": "dbfs:/FileStore/tables/circuits-2.csv"
}
],
"output": {
"sourceType": "csv",
"source": "dbfs:/FileStore/tables/ind-2.csv"
}
},
"graph": {
"nodes": [
{
"_id": "fdd38b4a-493f-5b9e-b462-28c07ac7fc07:op-1",
"_type": "Read",
"name": "LogicalRelation",
"properties": "null"
},
{
"_id": "fdd38b4a-493f-5b9e-b462-28c07ac7fc07:op-0",
"_type": "Write",
"name": "InsertIntoHadoopFsRelationCommand",
"properties": "null"
}
],
"edges": [
{
"source": "fdd38b4a-493f-5b9e-b462-28c07ac7fc07:op-1",
"target": "fdd38b4a-493f-5b9e-b462-28c07ac7fc07:op-0"
}
]
}
}
jsonSample = [
{
'class': 'Year 1',
'student count': 20,
'room': 'Yellow',
'info': {
'teachers': {
'math': 'Rick Scott',
'physics': 'Elon Mask'
}
},
'students': [
{
'name': 'Tom',
'sex': 'M',
'grades': { 'math': 66, 'physics': 77 }
},
{
'name': 'James',
'sex': 'M',
'grades': { 'math': 80, 'physics': 78 }
},
]
},
{
'class': 'Year 2',
'student count': 25,
'room': 'Blue',
'info': {
'teachers': {
'math': 'Alan Turing',
'physics': 'Albert Einstein'
}
},
'students': [
{ 'name': 'Tony', 'sex': 'M' },
{ 'name': 'Jacqueline', 'sex': 'F' },
]
},
]
df=pd.json_normalize(json_obj)
#
#
#
# #print(tabulate(df, headers='keys', tablefmt='psql'))
#
#
# result=pd.json_normalize(json_obj, record_path=['executionPlan','extra','attributes'])
#
#
#
# #df=pd.json_normalize(json_obj, record_path=['executionPlan.extra.attributes'],meta=['executionPlan', ['_id', 'name']],)
#
# print(tabulate(result, headers='keys', tablefmt='psql'))
#
# result=pd.json_normalize(json_obj, record_path=['executionPlan','extra','dataTypes'])
# print(tabulate(result, headers='keys', tablefmt='psql'))
#
# dx=flatten(json_obj)
#
# result=pd.json_normalize(dx)
# print(tabulate(result, headers='keys', tablefmt='psql'))
import pandas as pd
d1 = {
"id": [420, 380, 390],
"duration": ['50', '40', '45']
}
d2 = {
"id": [420, 380],
"duration": ['50', '70']
}
#load data into a DataFrame object:
df1 = pd.DataFrame(d1)
df2 = pd.DataFrame(d2)
print(tabulate(df1, headers='keys', tablefmt='psql'))
print(tabulate(df2, headers='keys', tablefmt='psql'))
#print(df)
result=(pd.merge(df1, df2, left_on='id', right_on='id', how='left'))
#new_df = pd.merge(A_df, B_df, how='left', left_on=['A_c1','c2'], right_on = ['B_c1','c2'])
rs=result[['duration_y']]
print(tabulate(result, headers='keys', tablefmt='psql'))
print(tabulate(rs, headers='keys', tablefmt='psql'))
# df=pd.DataFrame(dx,index=[0])
#
# print(tabulate(df, headers='keys', tablefmt='psql'))
# dfx=pd.json_normalize(dic)
#
# print(tabulate(dfx, headers='keys', tablefmt='psql'))
#
#dic_flattened = [flatten(d) for d in dic]
#
#
#
#df = pd.DataFrame(dx)
#df.head()