-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
549 lines (498 loc) · 29 KB
/
script.js
File metadata and controls
549 lines (498 loc) · 29 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
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
// Load data
let variable = document.getElementById("variable");
let loadData = `
conn = sqlite3.connect('data/sakila.db')
df = pd.read_sql('''
SELECT
rental.rental_id, rental.rental_date, rental.return_date,
customer.last_name AS customer_lastname,
store.store_id,
city.city AS rental_store_city,
film.title AS film_title, film.rental_duration AS film_rental_duration,
film.rental_rate AS film_rental_rate, film.replacement_cost AS film_replacement_cost,
film.rating AS film_rating
FROM rental
INNER JOIN customer ON rental.customer_id == customer.customer_id
INNER JOIN inventory ON rental.inventory_id == inventory.inventory_id
INNER JOIN store ON inventory.store_id == store.store_id
INNER JOIN address ON store.address_id == address.address_id
INNER JOIN city ON address.city_id == city.city_id
INNER JOIN film ON inventory.film_id == film.film_id
;
''', conn, index_col='rental_id', parse_dates=['rental_date', 'return_date'])
`;
const data = (stat) => {
if (variable.value === "") {
return alert("Please enter a variable name in the 'variable' field, in the 'Load data' section.");
} else {
document.editor.textbox.value+= "\n" + document.editor.variable.value + "." + stat;
}
}
// Numerical analysis and visualisation
let column = document.getElementById("column");
let axis = document.getElementById("plot-variable");
let analysis;
let mostValueCounts = document.getElementById("mostValueCounts");
const describe = (stat) => {
if (variable.value === "") {
return alert("Please enter a variable name in the 'variable' field, in the 'Load data' section.");
} else {
document.editor.textbox.value+= column.value === "" ? "\n" + document.editor.variable.value + "." + stat + "()\n" : "\n" + document.editor.variable.value + "['" + document.editor.column.value + "']." + stat + "()\n";
}
}
const meanMedian = (stat) => {
if (variable.value === "") {
return alert("Please enter a variable name in the 'variable' field, in the 'Load data' section.");
} else if (column.value === "") {
return alert("Please add a column name in the 'column' field, in the 'Numerical analysis and visualisation' section.");
} else {
document.editor.textbox.value+= "\n" + variable.value + "['" + column.value + "']." + stat + "()";
}
}
const analyseBox = (stat) => {
if (variable.value === "") {
return alert("Please enter a variable name in the 'variable' field, in the 'Load data' section.");
} else if (column.value === "") {
return alert("Please add a column name in the 'column' field, in the 'Numerical analysis and visualisation' section.");
} else if (axis.value !== "") {
document.editor.textbox.value+= "\n" + axis.value + " = " + variable.value + "['" + column.value + "'].plot(kind='" + stat + "', vert=False, figsize=(14,6))";
} else {
document.editor.textbox.value+= "\n" + variable.value + "['" + column.value + "'].plot(kind='" + stat + "', vert=False, figsize=(14,6))";
}
}
const densityHist = (stat) => {
if (variable.value === "") {
return alert("Please enter a variable name in the 'variable' field, in the 'Load data' section.");
} else if (column.value === "") {
return alert("Please add a column name in the 'column' field, in the 'Numerical analysis and visualisation' section.");
} else if (axis.value !== "") {
document.editor.textbox.value+= "\n" + axis.value + " = " + variable.value + "['" + column.value + "'].plot(kind='" + stat + "', figsize=(14,6))";
} else {
document.editor.textbox.value+= "\n" + variable.value + "['" + column.value + "'].plot(kind='" + stat + "', figsize=(14,6))";
}
}
const colour = () => {
if (variable.value === "") {
return alert("Please enter a variable name in the 'variable' field, in the 'Load data' section.");
} else if (analysis === undefined) {
return alert("Please select an analysis type.");
} else if (column.value === "") {
return alert("Please add a column name in the 'column' field, in the 'Numerical analysis and visualisation' section.");
} else if (axis.value === "") {
return alert("Please enter an axis name in the 'axis' field, in the 'Numerical analysis and visualisation' section.");
} else {
document.editor.textbox.value+="\n" + document.editor.axis.value + ".axvline(" + document.editor.variable.value + "['" + document.editor.column.value + "']." + analysis + "(), color='" + document.editor.colours.value + "')";
}
}
const round = () => {
if (mostValueCounts.value === "") {
return alert("Please add a number in the 'number' field, in the 'Categorical analysis and visualisation' section.");
} else {
document.editor.textbox.value+='.round(' + mostValueCounts.value + ')';
}
}
const axisLabel = (stat) => {
if (axis.value === "") {
return alert("Please enter an axis name in the 'axis' field, in the 'Numerical analysis and visualisation' section.");
} else if (label.value === '') {
return alert("Please enter a label name in the 'label' field, in the 'Numerical analysis and visualisation' section.");
} else {
document.editor.textbox.value+= "\n" + document.editor.axis.value + ".set_" + stat.replace(" ", "") + "('" + document.editor.label.value + "')";
}
}
// Categorical analysis and visualisation
const categoricalValueCounts = (stat) => {
if (variable.value === "") {
return alert("Please enter a variable name in the 'variable' field, in the 'Load data' section.");
} else if (column.value === "") {
return alert("Please add a column name in the 'column' field, in the 'Numerical analysis and visualisation' section.");
} else {
let head = mostValueCounts.value;
if (head) {
document.editor.textbox.value+="\n" + variable.value + "['" + column.value + "']." + stat.replace(' ', '_') + "().head(" + head + ")";
} else {
document.editor.textbox.value+="\n" + variable.value + "['" + column.value + "']." + stat.replace(' ', '_') + "()";
}
}
}
const categoricalBar = (stat) => {
if (variable.value === "") {
return alert("Please enter a variable name in the 'variable' field, in the 'Load data' section.");
} else if (column.value === "") {
return alert("Please add a column name in the 'column' field, in the 'Numerical analysis and visualisation' section.");
} else {
let head = mostValueCounts.value;
if (head) {
document.editor.textbox.value+="\n" + variable.value + "['" + column.value + "'].value_counts().head(" + head + ").plot(kind='" + stat + "', figsize=(14,6))";
} else {
document.editor.textbox.value+="\n" + variable.value + "['" + column.value + "'].value_counts().plot(kind='" + stat + "', figsize=(14,6))";
}
}
}
const categoricalPie = (stat) => {
if (variable.value === "") {
return alert("Please enter a variable name in the 'variable' field, in the 'Load data' section.");
} else if (column.value === "") {
return alert("Please add a column name in the 'column' field, in the 'Numerical analysis and visualisation' section.");
} else {
let head = mostValueCounts.value;
if (head) {
document.editor.textbox.value+="\n" + variable.value + "['" + column.value + "'].value_counts().head(" + head + ").plot(kind='" + stat + "', figsize=(6,6))";
} else {
document.editor.textbox.value+="\n" + variable.value + "['" + column.value + "'].value_counts().plot(kind='" + stat + "', figsize=(6,6))";
}
}
}
// Correlation between columns
let correlation = document.getElementById("correlation");
let figure = document.getElementById("figure");
let scatterX = document.getElementById("scatterX");
let scatterY = document.getElementById("scatterY");
let plots = document.getElementById("plots");
let boxPlotAxis = document.getElementById("boxPlotAxis");
let boxPlotCatergory = document.getElementById("boxPlotsCategory");
let boxPlotCatergoryValue = document.getElementById("boxPlotCategoryValue");
let boxPlotCategoryColumnA = document.getElementById("boxPlotCategoryColumnA");
let boxPlotCategoryColumnB = document.getElementById("boxPlotCategoryColumnB");
let label = document.getElementById("label");
let newColumn = document.getElementById("newColumn");
let columnA = document.getElementById("columnA");
let columnB = document.getElementById("columnB");
let groupA = document.getElementById("groupA");
let groupB = document.getElementById("groupB");
const matshow = (stat) => {
if (correlation.value === "") {
return alert("Please enter a name in the 'correlation field', in the 'Correlation between columns' section.");
} else if (figure.value === "") {
return alert("Please enter a name in the 'figure' field, in the 'Correlation between columns' section.");
} else {
document.editor.textbox.value+="\nplt." + stat + "(" + document.editor.correlation.value + ", cmap='RdBu', fignum=" + document.editor.figure.value + ".number)";
}
}
const scatters = (stat) => {
if (variable.value === "") {
return alert("Please enter a variable name in the 'variable' field, in the 'Load data' section.");
} else if (scatterX.value === "") {
return alert("Please enter a value in the 'scatter x' field, in the 'Correlation between columns' section.");
} else if (scatterY.value === "") {
return alert("Please enter a value in the 'scatter y' field, in the 'Correlation between columns' section.");
} else {
return document.editor.textbox.value+="\n" + document.editor.variable.value + ".plot(kind='" + stat + "', x='" + document.editor.scatterX.value + "', y='" + document.editor.scatterY.value + "', figsize=(6,6))";
}
}
const boxPlot = () => {
if (variable.value === "") {
return alert("Please enter a variable name in the 'variable' field, in the 'Load data' section.");
} else if (plots.value === "") {
return alert("Please enter at least one category into the 'boxplot categories' field, in the 'Correlation between columns' section.");
} else if (boxPlotAxis.value === "") {
return alert("Please enter a name in the 'boxplot axis' field, in the 'Correlation between columns' section.");
} else {
return document.editor.textbox.value+="\n" + boxPlotAxis.value + " = " + document.editor.variable.value + "[[" + document.editor.boxplotCats.value.split(',').map(el => `'${el.replace(' ', '')}'`).join().replaceAll(',', ', ') + "]].boxplot(by='" + document.editor.boxplotCats.value.split(',')[document.editor.boxplotCats.value.split(',').length - 1].replace(' ', '') + "', figsize=(14,6))\n" + boxPlotAxis.value + ".set_ylabel('" + document.editor.boxplotCats.value.split(',')[0] + "')";
}
}
const boxPlotGrouped = () => {
if (variable.value === '') {
return alert("Please enter a variable name in the 'variable' field, in the 'Load data' section.");
} else if (boxPlotCategory.value === '') {
return alert("Please enter a category in the 'category' field, in the 'Correlation between columns' section.");
} else if (boxPlotCategoryValue.value === '') {
return alert("Please enter a category value in the 'value' field, in the 'Correlation between columns' section.");
} else if (boxPlotCategoryColumnA.value === '') {
return alert("Please enter a category in the 'column A' field, in the 'Correlation between columns' section.");
} else if (boxPlotCategoryColumnB.value === '') {
return alert("Please enter a category in the 'column B' field, in the 'Correlation between columns' section.");
} else if (boxPlotAxis.value === '') {
return alert("Please enter a name in the 'boxplot axis' field, in the 'Correlation between columns' section.");
} else {
return document.editor.textbox.value+= "\n" + boxPlotAxis.value + " = " + variable.value + ".loc[" + variable.value + "['" + boxPlotCategory.value + "'] == " + boxPlotCategoryValue.value + ", ['" + boxPlotCategoryColumnA.value + "', '" + boxPlotCategoryColumnB.value + "']]" + "\n" + boxPlotAxis.value + ".boxplot(by='" + boxPlotCategoryColumnB.value + "', figsize=(14,6))";
}
}
const box = () => {
if (variable.value === "") {
return alert("Please enter a variable name in the 'variable' field, in the 'Load data' section.");
} else if (plots.value === "") {
return alert("Please enter at least one category into the 'boxplot categories' field, in the 'Correlation between columns' section.");
} else {
return document.editor.textbox.value+="\n" + document.editor.variable.value + "[[" + document.editor.boxplotCats.value.split(',').map(el => `'${el.replace(' ', '')}'`) + "]].plot(kind='box', subplots=True, layout=(2,3), figsize=(14,8))";
}
}
// Add and calculate a new column
let scatterAxis = document.getElementById("scatterAxis");
const columnWrangle = (stat) => {
let bin = "";
if (stat === "+") {
bin = " bins=100,";
}
if (variable.value === '') {
return alert("Please enter a variable name in the 'variable' field, in the 'Load data' section.");
} else if (newColumn.value === "") {
return alert("Please add a column name in the 'new column' field in the 'Column Wrangling' section.");
} else if (columnA.value === "") {
return alert("Please add a primary column name in the 'primary column' field, in the 'Column Wrangling' section.");
} else if (columnB.value === "") {
return alert("Please add a secondary column name in the 'secondary column' field, in the 'Column Wrangling' section.");
} else {
document.editor.textbox.value+="\n" + variable.value + "['" + newColumn.value + "'] = " + variable.value + "['" + columnA.value + "'] " + stat + " " + variable.value + "['" + columnB.value + "']\n" + variable.value + "['" + newColumn.value + "'].head()" + "\n" + variable.value + "['" + newColumn.value + "'].plot(kind='hist'," + bin + " figsize=(14,6))";
}
}
const columnPlot = (stat) => {
if (variable.value === "") {
return alert("Please enter a variable name in the 'variable' field, in the 'Load data' section.");
} else if (newColumn.value === "") {
return alert("Please add a column name in the 'new column' field in the 'Column Wrangling' section.");
} else {
document.editor.textbox.value+="\n" + variable.value + "['" + newColumn.value + "'].plot(kind='" + stat + "', figsize=(14,6))";
}
}
const columnScatter = (stat) => {
if (variable.value === '') {
return alert("Please enter a variable name in the 'Load data' section");
} else if (columnA.value === "") {
return alert("Please add a primary column name in the 'primary column' field, in the 'Column Wrangling' section.");
} else if (scatterAxis.value === "") {
return alert("Please add a scatter column name in the 'scatter' field, in the 'Add and calculate a new column' section.");
} else {
document.editor.textbox.value+="\n" + variable.value + ".plot(kind='" + stat + "', x='" + columnA.value + "', y='" + scatterAxis.value + "', figsize=(6,6))";
}
}
// Modify existing column
let modify = document.getElementById("modify");
const columnWrangleModify = (stat) => {
if (variable.value === "") {
return alert("Please enter a variable name in the 'variable' field, in the 'Load data' section.");
} else if (columnA.value === "") {
return alert("Please add a primary column name in the 'primary column' field, in the 'Column Wrangling' section.");
} else if (modify.value === "") {
return alert("Please add a value in the 'modify' field, in the 'Modify existing column' section.");
} else if (!Number(modify.value) && Number(modify.value) != "0") {
return alert("Please add a valid number in the 'modify' field, in the 'Modify existing column'.");
} else {
document.editor.textbox.value+="\n" + variable.value + "['" + columnA.value + "'] " + stat + "= " + modify.value + "\n" + variable.value + "['" + columnA.value + "'].head()";
}
}
// Selection and indexing
let selectionGrouping = document.getElementById("selectionGrouping");
const individualSelection = () => {
if (variable.value === "") {
return alert("Please enter a variable name in the 'variable' field, in the 'Load data' section.");
} else if (columnA.value === "") {
return alert("Please add a primary column name in the 'primary column' field, in the 'Column Wrangling' section.");
} else if (columnB.value === "") {
return alert("Please add a secondary column name in the 'secondary column' field, in the 'Column Wrangling' section.");
} else {
document.editor.textbox.value+="\n" + variable.value + ".loc[" + variable.value + "['" + columnA.value + "'] == '" + columnB.value + "']" + "\n" + variable.value + "['" + columnA.value + "'].head()";
}
}
const groupSelectionMean = () => {
if (variable.value === "") {
return alert("Please enter a variable name in the 'variable' field, in the 'Load data' section.");
} else if (columnA.value === "") {
return alert("Please add a primary column name in the 'primary column' field, in the 'Column Wrangling' section.");
} else if (groupA.value === "") {
return alert("Please add a category in 'group A' field, in the 'Different category' subsection.");
} else if (groupB.value === "") {
return alert("Please add a category in 'group B' field, in the 'Different category' subsection.");
} else {
document.editor.textbox.value+="\n" + variable.value + ".loc[" + variable.value + "['" + columnA.value + "'] == '" + groupA.value + "', '" + groupB.value + "'].mean()";
}
}
const meanSelection = () => {
if (variable.value === "") {
return alert("Please enter a variable name in the 'variable' field, in the 'Load data' section.");
} else if (columnA.value === "") {
return alert("Please add a primary column name in the 'primary column' field, in the 'Column Wrangling' section.");
} else if (groupA.value === "") {
return alert("Please add a category in 'group A' field, in the 'Different category' subsection.");
} else if (columnB.value === "") {
return alert("Please add a secondary column name in the 'secondary column' field, in the 'Column Wrangling' section.");
} else if (groupB.value === "") {
return alert("Please add a category in 'group B' field, in the 'Different category' subsection.");
} else if (newColumn.value === "") {
return alert("Please add a column name in the 'new column' field in the 'Column Wrangling' section.");
} else {
document.editor.textbox.value+="\n" + variable.value + ".loc[(" + variable.value + "['" + columnA.value + "'] == '" + groupA.value + "') & (" + variable.value + "['" + columnB.value + "'] == '" + groupB.value + "'), '" + newColumn.value + "'].mean()";
}
}
const meanMaxSelection = () => {
if (variable.value === "") {
return alert("Please enter a variable name in the 'variable' field, in the 'Load data' section.");
} else if (mostValueCounts.value === "") {
return alert("Please add a number in the 'number' field, in the 'Categorical analysis and visualisation' section.");
} else if (columnA.value === "") {
return alert("Please add a primary column name in the 'primary column' field, in the 'Column Wrangling' section.");
} else if (columnB.value === "") {
return alert("Please add a secondary column name in the 'secondary column' field, in the 'Column Wrangling' section.");
} else {
document.editor.textbox.value+="\n" + variable.value + ".loc[" + variable.value + "['" + columnA.value + "'] > " + mostValueCounts.value + ", '" + columnB.value + "'].mean()";
}
}
const meanMinSelection = () => {
if (variable.value === "") {
return alert("Please enter a variable name in the 'variable' field, in the 'Load data' section.");
} else if (mostValueCounts.value === "") {
return alert("Please add a number in the 'number' field, in the 'Categorical analysis and visualisation' section.");
} else if (columnB.value === "") {
return alert("Please add a secondary column name in the 'secondary column' field, in the 'Column Wrangling' section.");
} else {
document.editor.textbox.value+="\n" + variable.value + ".loc[" + variable.value + "['" + columnA.value + "'] < " + mostValueCounts.value + ", '" + columnB.value + "'].mean()";
}
}
const percentageSelection = () => {
if (variable.value === "") {
return alert("Please enter a variable name in the 'variable' field, in the 'Load data' section.");
} else if (columnA.value === "") {
return alert("Please add a primary column name in the 'primary column' field, in the 'Column Wrangling' section.");
} else if (groupA.value === "") {
return alert("Please add a category in 'group A' field, in the 'Different category' subsection.");
} else if (newColumn.value === "") {
return alert("Please add a column name in the 'new column' field in the 'Column Wrangling' section.");
} else if (selectionPercentage.value === "") {
return alert("Please add a percentage in the 'percentage' field, in the 'Mean' subsection.");
} else {
let percentage = selectionPercentage.value;
let percentageNegative = false;
if (parseFloat(percentage) < 0 ) {
percentageNegative = true;
let negative = Math.abs(percentage);
percentage = percentage.replace("-", "");
}
if (!percentage.includes(".")) {
percentage = parseFloat(percentage);
if (parseFloat(percentage) > 0 && parseFloat(percentage) < 10 ) {
percentage = "0.0" + percentage;
} else if (parseFloat(percentage) < 100 ) {
percentage = "0." + percentage;
} else {
percentage = parseFloat(percentage) / 100;
}
}
if (percentageNegative) {
percentage = "-" + percentage;
}
document.editor.textbox.value+="\n" + variable.value + ".loc[" + variable.value + "['" + columnA.value + "'] == '" + groupA.value + "', '" + newColumn.value + "'] *= " + percentage;
}
}
const maxSelection = () => {
if (variable.value === "") {
return alert("Please enter a variable name in the 'variable' field, in the 'Load data' section.");
} else {
document.editor.textbox.value+="\n" + variable.value + ".loc[" + variable.value + "['" + columnA.value + "'] == " + variable.value + "['" + columnA.value + "'].max()]";
}
}
const sort = () => {
if (variable.value === "") {
return alert("Please enter a variable name in the 'variable' field, in the 'Load data' section.");
} else if (mostValueCounts.value === "") {
return alert("Please add a number in the 'number' field, in the 'Categorical analysis and visualisation' section.");
} else {
document.editor.textbox.value+="\n" + variable.value + ".sort_values(['" + columnA.value + "'], ascending=False).head(" + mostValueCounts.value + ")";
}
}
const valueCountSelection = () => {
if (variable.value === "") {
return alert("Please enter a variable name in the 'variable' field, in the 'Load data' section.");
} else if (columnA.value === "") {
return alert("Please add a primary column name in the 'primary column' field, in the 'Column Wrangling' section.");
} else if (groupA.value === "") {
return alert("Please add a category in 'group A' field, in the 'Different category' subsection.");
} else if (newColumn.value === "") {
return alert("Please add a column name in the 'new column' field in the 'Column Wrangling' section.");
} else {
document.editor.textbox.value+="\n" + variable.value + ".loc[" + variable.value + "['" + columnA.value + "'] == '" + groupA.value + "', '" + newColumn.value + "'].value_counts()";
}
}
const numberSelectionSingle = () => {
if (variable.value === "") {
return alert("Please enter a variable name in the 'variable' field, in the 'Load data' section.");
} else if (columnA.value === "") {
return alert("Please add a primary column name in the 'primary column' field, in the 'Column Wrangling' section.");
} else if (groupA.value === "") {
return alert("Please add a category in 'group A' field, in the 'Different category' subsection.");
} else {
document.editor.textbox.value+="\n" + variable.value + ".loc[(" + variable.value + "['" + columnA.value + "'] == '" + groupA.value + "')].shape[0]";
}
}
const numberSelectionDoubleSame = () => {
if (variable.value === "") {
return alert("Please enter a variable name in the 'variable' field, in the 'Load data' section.");
} else if (columnA.value === "") {
return alert("Please add a primary column name in the 'primary column' field, in the 'Column Wrangling' section.");
} else if (groupA.value === "") {
return alert("Please add a category in 'group A' field, in the 'Different category' subsection.");
} else if (groupB.value === "") {
return alert("Please add a category in 'group B' field, in the 'Different category' subsection.");
} else {
document.editor.textbox.value+="\n" + variable.value + ".loc[(" + variable.value + "[" + columnA.value + "'] == '" + groupA.value + "') " + selectionGrouping.value + " (" + variable.value + "['" + columnA.value + "'] == '" + groupB.value + "')].shape[0]";
}
}
const numberSelectionDoubleDiff = () => {
if (variable.value === "") {
return alert("Please enter a variable name in the 'variable' field, in the 'Load data' section.");
} else if (columnA.value === "") {
return alert("Please add a primary column name in the 'primary column' field, in the 'Column Wrangling' section.");
} else if (columnB.value === "") {
return alert("Please add a secondary column name in the 'secondary column' field, in the 'Column Wrangling' section.");
} else if (groupA.value === "") {
return alert("Please add a category in 'group A' field, in the 'Different category' subsection.");
} else if (groupB.value === "") {
return alert("Please add a category in 'group B' field, in the 'Different category' subsection.");
} else {
document.editor.textbox.value+="\n" + variable.value + ".loc[(" + variable.value + "['" + columnA.value + "'] == '" + groupA.value + "') " + selectionGrouping.value + " (" + variable.value + "['" + columnB.value + "'] == '" + groupB.value + "')].shape[0]";
}
}
const numberSelectionDoubleDiffIsIn = () => {
if (variable.value === "") {
return alert("Please enter a variable name in the 'variable' field, in the 'Load data' section.");
} else if (columnA.value === "") {
return alert("Please add a primary column name in the 'primary column' field, in the 'Column Wrangling' section.");
} else if (columnB.value === "") {
return alert("Please add a secondary column name in the 'secondary column' field, in the 'Column Wrangling' section.");
} else if (groupA.value === "") {
return alert("Please add a category in 'group A' field, in the 'Different category' subsection.");
} else if (groupB.value === "") {
return alert("Please add a category in 'group B' field, in the 'Different category' subsection.");
} else {
document.editor.textbox.value+="\n" + variable.value + ".loc[(" + variable.value + "['" + columnA.value + "'] == '" + groupA.value + "') " + selectionGrouping.value + " (" + variable.value + "['" + columnB.value + "'].isin([" + groupB.value.split(',').map(el => `'${el.replace(' ', '')}'`).join().replaceAll(',', ', ') + "]" + "))].shape[0]";
}
}
// Dates
let dateColumn = document.getElementById("dateColumn");
const date = () => {
if (variable.value === "") {
return alert("Please enter a variable name in the 'variable' field, in the 'Load data' section.");
} else if (dateColumn.value === "") {
return alert("Please enter a date in the 'date column' field, in the 'Date' section.");
} else {
document.editor.textbox.value+="\n" + variable.value + "['" + dateColumn.value + "'] = " + variable.value + "[['Year', 'Month', 'Day']].apply(lambda x: '{}-{}-{}'.format(x[0], x[1], x[2]), axis=1)";
}
}
const dateDiff = () => {
if (variable.value === "") {
return alert("Please enter a variable name in the 'variable' field, in the 'Load data' section.");
} else if (dateNewColumn.value === '') {
return alert("Please enter a new column name in the 'new column' field, in the 'Date' section.");
} else if (dateColumn.value.split(",").length !== 2) {
return alert("Please enter two dates in the 'date column' field, in the 'Date' section.");
} else {
document.editor.textbox.value+="\n" + variable.value + "['" + dateNewColumn.value + "'] = " + variable.value + "[[" + dateColumn.value.split(',').map(el => `'${el.replace(' ', '')}'`) + "]].apply(lambda x: (x[1] - x[0]).days, axis=1)";
}
}
const parseDate = () => {
if (variable.value === "") {
return alert("Please enter a variable name in the 'variable' field, in the 'Load data' section.");
} else if (dateColumn.value === "") {
return alert("Please enter a date in the 'date column' field, in the 'Date' section.");
} else {
document.editor.textbox.value+="\n" + variable.value + "['" + dateColumn.value + "'] = pd.to_datetime(" + variable.value + "['" + dateColumn.value + "'])";
}
}
const lineDate = () => {
if (variable.value === "") {
return alert("Please enter a variable name in the 'variable' field, in the 'Load data' section.");
} else if (dateColumn.value === '') {
return alert("Please enter a date in the 'date column' field, in the 'Date' section.");
} else {
document.editor.textbox.value+="\n" + variable.value + "['" + dateColumn.value + "'].value_counts().plot(kind='line', figsize=(14,6))";
}
}