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:

  • $\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$ Commented Aug 6, 2014 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
    Commented Aug 6, 2014 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.
    Commented Aug 6, 2014 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$ Commented May 14, 2015 at 12:47

5 Answers 5


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]

Another way to accomplish this:

Export["text.csv", Transpose@Normal@ds[{Keys, Values}]]

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", _] -> ""}),

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


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


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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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