forked from ladymeyy/tablecloth-talks-and-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.clj
130 lines (90 loc) · 3.32 KB
/
core.clj
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
(ns tablecloth-workshop.core
(:require
[tablecloth.api :as tc]
[tech.v3.dataset.print :as print]
[tech.v3.datatype.functional :as fun]
[clojure.string :as string]))
(def pokemon-ds-path "resources/pokemon.csv")
(def clean-pokemon-ds-path "resources/clean-pokemon.csv")
(def combats-ds-path "resources/combats.csv")
; 1.Load the csv
; 2.Clean the data
; 3. present the table
(-> pokemon-ds-path
(slurp)
(string/replace #"'" "")
(->>(spit clean-pokemon-ds-path)))
(def pokemon-ds (-> pokemon-ds-path
(tc/dataset {:key-fn keyword})))
(def combats-ds (-> combats-ds-path
(tc/dataset {:key-fn keyword})))
;Select specific columns
(-> pokemon-ds
(tc/select-columns [:pokedex_number :name :height_m :weight_kg :generation :is_legendary]))
;Find shortest pokemon
(-> pokemon-ds
(tc/select-columns [:pokedex_number :name :height_m :weight_kg :generation :is_legendary])
(tc/order-by [:height_m]))
;Find tallest pokemon
(-> pokemon-ds
(tc/select-columns [:pokedex_number :name :height_m :weight_kg :generation :is_legendary])
(tc/order-by [:height_m] :desc))
;Which pokemon are you?
;--------------
;SELECT pokedex_number, name, height_m, weight_kg
;FROM p pokemon.csv
;LEFT JOIN (SELECT COUNT(winner) winner
; FROM battles
; GROUP BY winner
; ) AS b WHERE b.winner = p.pokedex_number
;WHERE p.height BETWEEN height-0.5 AND height+0.5
;AND p.weight BETWEEN weight-0.5 AND weight+0.5
;User info
(def user-height 0.5)
(def user-weight 50)
;These functions won't check for NULL values - produces an error
(defn weight-in-range? [row]
(> (+ user-weight 1) (:weight_kg row) (- user-weight 1)))
(defn height-in-range? [row]
(> (+ user-height 0.5) (:height_m row) (- user-height 0.5)))
;Get a row from csv as a map and check if weight & height is in pre-defined range
;This also checks null value.
(defn weight-in-range? [row]
(and (:weight_kg row)
(> (+ user-weight 1) (:weight_kg row) (- user-weight 1))))
(defn height-in-range? [row]
(and (:height_m row)
(> (+ user-height 0.5) (:height_m row) (- user-height 0.5))))
;Get pokemon in height range
(-> pokemon-ds
(tc/select-columns [:pokedex_number :name :height_m :weight_kg ])
(tc/select-rows height-in-range?)
)
;Get pokemon in height and weight range
(-> pokemon-ds
(tc/select-columns [:pokedex_number :name :height_m :weight_kg ])
(tc/select-rows (fn [row]
(and (weight-in-range? row) (height-in-range? row))))
)
;group by winner
(-> combats-ds
(tc/group-by [:Winner])
)
;Adds another column to the table: Each row holds the size of the group it belongs to
(-> combats-ds
(tc/group-by [:Winner])
(tc/aggregate {:number-of-wins tc/row-count} )
)
;Join will be done by with matching columns by their name so rename :winner to :pokedex_number
(def number-of-wins
(-> combats-ds
(tc/group-by [:Winner])
(tc/aggregate {:number-of-wins tc/row-count} )
(tc/rename-columns {:Winner :pokedex_number})
))
(def pokemon-filtered
(-> pokemon-ds
(tc/select-columns [:pokedex_number :name :height_m :weight_kg ])
(tc/select-rows (fn [row]
(and (weight-in-range? row) (height-in-range? row))))
(tc/left-join number-of-wins :pokedex_number)))