Example: CSV file with 4 headers: a,b,c,d. How do I only "pick" columns b & d for example, so I end up with a q table with two columns with the b & d headers?
Asked
Active
Viewed 1,646 times
1 Answers
3
For columns that you want to skip, use the space character in the "types" string. So to pick 'b' and 'd' columns:
q)\cat test.csv
"a,b,c,d"
"1,blah,3,4"
q)
q)(" S J";enlist csv) 0: `:test.csv
b d
------
blah 4

James Little
- 1,964
- 13
- 12
-
Follow up question: I guess there is no way to actually use the name of the header to pick up the column? If I have 100s of columns and only wanted to use the actual header name as a string to pick up the given column. Unless I write up a sep function to parse the first header row and get the index of the desired column header. – Alim Hasanov May 03 '17 at 12:18
-
1Might not be the most efficient solution, but you could load all the data and then select the columns you need, e.g. `select b,d from ("****";enlist csv) 0: \`:test.csv` – James Little May 03 '17 at 12:21
-
Thank you . One last question. How do I import ALL the columns as strings, without having to type each column type as ("SSSSSS...") I want to onlu do something like ("all columns data type";enlist csv). Don't want to have to type each columns data type. – Alim Hasanov May 03 '17 at 12:38
-
If you know how many columns are in the table (or count them beforehand), just use take `#`, e.g. `(4#"*";enlist csv) 0: \`:test.csv`. You should be wary of loading all strings as symbols though, unless they are repeating strings. See more [here](http://www.timestored.com/kdb-guides/strings-symbols-enumeration#when-to-use) – James Little May 03 '17 at 13:09
-
thanks James yet again, thats what I was looking for – Alim Hasanov May 03 '17 at 15:57