-
Notifications
You must be signed in to change notification settings - Fork 2
/
table_manipulation.nu
349 lines (312 loc) · 7.08 KB
/
table_manipulation.nu
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
#get $in input if necessary
export def get-input [
inp #in variable
var #input variable
--name(-n) #get name of $inp
] {
if $name {
if ($var | is-empty) {$inp | get name} else {$var}
} else {
if ($var | is-empty) {$inp} else {$var}
}
}
#range to list
export def range2list [] {
each {||}
}
#ansi strip table
export def "ansi-strip-table" [] {
update cells {|cell|
if ($cell | describe) == string {
$cell | ansi strip
} else {
$cell
}
}
}
#add a hidden column with the content of the # column
export def indexify [
column_name?: string = 'index' #export default: index
] {
enumerate
| upsert $column_name {|el|
$el.index
}
| flatten
}
#returns a filtered table that has distinct values in the specified column
export def uniq-by [
column: string #the column to scan for duplicate values
] {
reduce { |item, acc|
if ($acc | any { |storedItem|
($storedItem | get $column) == ($item | get $column)
}) {
$acc
} else {
$acc | append $item
}
}
}
#get total sizes of ls output
export def sum-size [] {
get size | math sum
}
#table to record
export def table2record [] {
transpose -r -d
}
#calculates elements that are in list a but not in list b
export def setdiff [a,b] {
$a
| each {|n|
if $n not-in $b {$n}
}
}
#find index of a search term
export def "find-index" [name: string,default? = -1] {
$in
| indexify
| find $name
| try {
get index
} catch {
$default
}
}
#select column of a table (to table)
export def column [n] {
transpose
| select $n
| transpose
| select column1
| headers
}
#get column of a table (to list)
export def column2 [n] {
transpose
| get $n
| transpose
| get column1
| skip 1
}
#join 2 lists
export def union [a: list, b: list] {
$a
| append $b
| uniq
}
#intersection between two lists
export def intersect [a:list, b:list] {
let a = $a | uniq | sort
let b = $b | uniq | sort
let n_a = $a | length
let n_b = $b | length
mut i = 0
mut j = 0
mut c = []
while ($i < $n_a and $j < $n_b) {
if ($a | get $i) < ($b | get $j) {
$i = $i + 1
} else if ($b | get $j) < ($a | get $i) {
$j = $j + 1
} else {
$c = $c ++ ($b | get $j)
$i = $i + 1
$j = $j + 1
}
}
return $c
}
#select rows in a table from list of ints
export def get-rows [rows:list] {
$in
| enumerate
| where $it.index in $rows
| get item
}
# interactively select columns from a table
export def iselect [] {
let tgt = $in
let cols = ($tgt | columns)
let choices = ($cols | input list -m "Pick columns to get: ")
$tgt | select $choices
}
#default a whole table
export def default-table [value: any = null] {
mut input = $in
let cols = $input | columns
for $col in $cols {
$input = ($input | default $value $col)
}
$input
}
# table diff
export def table-diff [
$left: list<any>,
$right: list<any>,
--keys (-k): list<string> = [],
] {
let left = if ($left | describe) !~ '^table' { $left | wrap value } else { $left }
let right = if ($right | describe) !~ '^table' { $right | wrap value } else { $right }
let left_selected = ($left | select ...$keys)
let right_selected = ($right | select ...$keys)
let left_not_in_right = (
$left |
filter { |row| not (($row | select ...$keys) in $right_selected) }
)
let right_not_in_left = (
$right |
filter { |row| not (($row | select ...$keys) in $left_selected) }
)
(
$left_not_in_right | insert side '<='
) ++ (
$right_not_in_left | insert side '=>'
)
}
# filter by multiple where conditions simultaneous
# Example:
#
# ls | multiwhere { name: .txt, type: file }
export def multiwhere [maps: record]: table -> table {
let inp = $in
if ($inp | is-empty) {
return $inp
}
$maps
| items {|key, val| { col: $key, val: $val } }
| reduce --fold $inp {|map, acc|
$acc | filter {|x| ($x | get $map.col) =~ $map.val}
}
}
# sum lists of numbers
# Example:
# let a = [1 2 3]
#
# list-sum $a $a
# list-sum $a $a $a
export def list-sum [
...rest # list of lists of numbers
] {
let n = ($rest | length) - 1
mut sum = $rest.0 | zip $rest.1 | each {$in.0 + $in.1}
if $n < 2 {return $sum}
for i in 2..$n {
$sum = ($sum | (zip ($rest | get $i)) | each {$in.0 + $in.1} )
}
return $sum
}
# difference between 2 lists of numbers
# Example:
# let a = [1 2 3]
#
# list-sum $a $a
export def list-diff [
...rest # list of lists of numbers
] {
$rest.0 | zip $rest.1 | each {$in.0 - $in.1}
}
# group list
# Example:
# [1 1 2 2 3 4] | group list {$in mod 2 == 0}
export def group-list [cond: closure] {
zip ($in | each $cond)
| prepend [null]
| window 2
| each {|i|
let prev = ($i.0 | default $i.1)
let next = $i.1
if $prev.1 != $next.1 {
[null $next.0]
} else {
$next.0
}
}
| flatten
| split list null
}
#simple pivoting of a table without aggregation
#It's a process of summarizing data from a table into a new table by grouping values from one or more columns into new columns and then applying an aggregation function to the values in other columns.
#
#For instance:
#
#table_1:
#YEAR,ITEM,VALUE
#2000,case1,10
#2000,case2,20
#2000,case3,20
#2001,case1,20
#2001,case2,10
#2001,case3,50
#2003,case2,30
#2003,case1,50
#2004,case3,10
#2004,case2,39
#
#converts to table_2:
#ITEM,2000,2001,2003,2004
#case1,10,20,50,
#case2,20,10,30,39
#case3,20,50,,10
#
#via:
#
#$table_1 | pivot-table --columns [YEAR] --index [ITEM] --values [VALUE]
export def pivot-table [
table_1? #table to pivot
--columns(-c):list #column names for pivoting (new columns)
--index(-i):list #index names for pivoting (first new column)
--values(-v):list #values for pivoting (values in the new columns)
] {
let table_1 = if ($table_1 | is-empty) {$in} else {$table_1}
$table_1 | polars into-df | polars pivot -o $columns -i $index -v $values
}
#generates table with an unique constant value
export def const-table [
value:int #value to span
nrows:int #number of rows
--number_of_cols(-m):int #number of columns (generates colums c0, c1, etc)
--cols(-c):list #list of column names (it has precedence over -m)
] {
let ncols = if ($cols | is-empty) {
$number_of_cols
} else {
$cols | length
}
$value
| std repeat $ncols
| wrap dummy
| transpose -i
| std repeat $nrows
| flatten
| if ($cols | is-not-empty) {
$in | rename ...$cols
} else {
$in
}
}
#list of lists into table
export def lists2table [
list?:list
--column_name(-c):string = "c"
] {
let list = get-input $in $list
mut matrix = $list | get 0 | wrap $"($column_name)0"
for i in 1..(($list | length) - 1) {
$matrix = ($matrix | merge ($list | get $i | wrap $"($column_name)($i)"))
}
return $matrix
}
#checks to see if the elements in the first list are contained in the second list
#analog to polars is-in
#
#Example:
#
# let a = [[a]; [a] [b] [c] [d]]
# let b = [[a]; [a] [c]]
# $a | is-in $b
export def is-in [subset:list, all?:list] {
let all = get-input $in $all
$all | each {|x| $x in $subset}
}