12
$\begingroup$

I'm trying to Export a simple dataset with key-value pairs to a CSV

Export["n.csv", MyDataset, "CSV"]

This results in a single-line serialization:

Dataset[<|"A" -> 3757, "B" -> 426, "C" -> 193, ...."D" -> 1|>, TypeSystem`Assoc[TypeSystem`Atom[String], TypeSystem`Atom[Integer], 15], <|"Origin" -> HoldComplete[Sort[Dataset`DatasetHandle[265575026537634], Greater]], "ID" -> 109770792907938|>]

The desired result is:

A,3757
B,426
C,193
...
D,1
$\endgroup$
  • $\begingroup$ Are you looking for a way to make Export do the conversion or for a way to transform your dataset to a form that can be handled by Export? The latter should be easy; something with Normal and then converting the list to a table (various possible approaches). $\endgroup$ – Sjoerd C. de Vries Aug 6 '14 at 9:26
  • $\begingroup$ Oh, it doesn't suppport datasets:-) I can't seem to find a simple function to do the conversion, any tips? I just want the desired result (Also shown visually in Mathematica). $\endgroup$ – tovare Aug 6 '14 at 9:59
  • 1
    $\begingroup$ For your simple example this will do: ExportString[(ds // Normal // Normal) /. Rule -> List, "CSV"] (Replace ExportString with Export in your case though.) $\endgroup$ – C. E. Aug 6 '14 at 10:21
  • 2
    $\begingroup$ Its worth noting that both simple row and column based datasets now export cleanly to CSV in 10.1 but not 10.0.2. Complex datasets involving nested Assocations don't throw an error. $\endgroup$ – Gordon Coale May 14 '15 at 12:47
12
$\begingroup$

Here is one option:

ds = Dataset[<|"A" -> 3757, "B" -> 426, "C" -> 193, "D" -> 1|>]

enter image description here

Now, to create the CSV you can use:

Export["text.csv", List@@@Normal@Normal@ds]
$\endgroup$
5
$\begingroup$

Another way to accomplish this:

Export["text.csv", Transpose@Normal@ds[{Keys, Values}]]
$\endgroup$
1
$\begingroup$

There is a new function TextString in V10, and one option can be used to format the export of Association.

ds=Dataset[<|"A"->3757,"B"->426,"C"->193,"D"->1|>]
Export["test.txt",TextString[ds//Normal,AssociationFormat->{"","\n","",","}]]
Export["test.csv",TextString[ds//Normal,AssociationFormat->{"","\n","",","}],"TextDelimiters"->None]
$\endgroup$
1
$\begingroup$

As Gordon Coale points out Mathematica 10.1+ supports dataset export as CSV:

Export["test.csv", ds, "CSV"]

However, I found it to be unstable if there are missing values. Instead I use this script:

Export[ "data/test.csv",
  {Normal@Keys[ ds[1] ]} ~ Join ~
    (Normal@Values[ds] /. {Missing["KeyAbsent", _] -> ""}),
  "CSV"
]

It produces CSV files that are very similar to Mathematica's export.

$\endgroup$
0
$\begingroup$

I could not get the above to work. It put rules into the file and I just wanted values. Assuming that the Datasets holds a list of association objects as a simple table i.e. does not have multiple levels, I used

Export["output.csv", ds[Values[#]&]//Normal]]

Values is applied to every association object and Normal strips the head and table metadata out. There does not appear to be an operator version of Values.

$\endgroup$

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.