7
$\begingroup$

I might use this incorrectly. I want to use a .csv file that (as list) looks like:

{{"Group", "data 1", "data 2"}, {"a", 1, 5}, {"a", 2, 3}, {"a", 4, 5}, {"b", 8, 9}}

Now I want this as a Dataset, i.e. I try and import it with SemanticImport and would want to have as result

enter image description here

However, I can't get it to work with SemanticImport.

I can use the following code, but hoped SemanticImport would basically replace this:

Import["testdata.csv"];
dataDim = Dimensions@data;
a1 = Transpose@
   Table[data[[1, i]] -> data[[1 + j, i]], {i, dataDim[[2]]}, {j, 
     dataDim[[1]] - 1}];
testFullAss = Association[a1[[#]]] & /@ Range[dataDim[[1]] - 1];

Dataset[testFullAss]
$\endgroup$
  • $\begingroup$ If you can tell me how I can add a file to my question I will do. But basically, if you Export the list at the beginning at my question as a .csv file, you'll have the file. $\endgroup$ – Mockup Dungeon Sep 14 '14 at 17:32
10
$\begingroup$

Using

SemanticImport["testdata.csv"]

I get

Mathematica graphics

on the exported data you provided which is the dataset you seek. But SemanticImport has been reported to have a few bugs, maybe that's why you can't get it to work. In the meantime, and in between time, you can use the much cleaner approach to obtain your dataset after using Import on your file:

data = {{"Group", "data 1", "data 2"}, {"a", 1, 5}, {"a", 2, 3}, {"a", 4,  5}, {"b", 8, 9}};

Then:

Dataset[AssociationThread[First @ data, #] & /@ Rest @ data]

Mathematica graphics

$\endgroup$
  • $\begingroup$ OK, that's weird you get the correct result. But at least it in principle appears to work. Thanks for the much shorter code you provided, which I will use in the meantime. $\endgroup$ – Mockup Dungeon Sep 14 '14 at 17:50
  • $\begingroup$ @MockupDungeon. You're welcome :) $\endgroup$ – RunnyKine Sep 14 '14 at 17:51
  • $\begingroup$ Setting dset = Dataset[AssociationThread[First @ data, #] & /@ Rest @ data] and din = SemanticImport["data.csv"], note that dset === din gives False, because as the FullForms reveal, they have different values for Association[Rule["ID".... What's the best way to extract just the datasets, without their IDs, so as to be able to compare? $\endgroup$ – murray Sep 14 '14 at 18:59
  • $\begingroup$ @murray, that's a good observation. I hadn't noticed that. I'll look into it. $\endgroup$ – RunnyKine Sep 14 '14 at 19:29
  • $\begingroup$ @murray, One way will be to do Normal[dset] == Normal[din] which yields True as expected. $\endgroup$ – RunnyKine Sep 14 '14 at 19:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.