I have a dataset which includes strings and numbers. I want to export it in CSV with all numbers being printed with 2 decimals. I tried several functions like

mydataset=NumberForm[#, {Infinity, 2}] & /@ mydataset

or using $PrePrint command but did not succeed to do what I want. Thanks for your help.

  • $\begingroup$ What was not acceptable with the form you have tried? In my case it gives correct result. Export["test.csv", NumberForm[#, {Infinity, 2}] & /@ dataset]. Can you give some small subset of your dataset to be used as example. $\endgroup$ – ercegovac Oct 3 '17 at 9:48
  • $\begingroup$ I tested your export command with a very simple dataset (the first one appearing in the wolfram documentation) and it does not export correctly as CSV. You get a CSV file with associations but not the CSV form. $\endgroup$ – Porty Oct 3 '17 at 10:54

This should work

dataset[All, All, NumberForm[#, {Infinity, 2}] &] 
%[Values[#] & // Normal]
Export["test.csv", %]

which when applied to the following example

dataset = Dataset[{
   <|"a" -> 1, "b" -> "xman", "c" -> 1|>,
   <|"a" -> 2, "b" -> "bear", "c" -> N[Pi]|>,
   <|"a" -> 3, "b" -> "fear", "c" -> 3|>,
   <|"a" -> 4, "b" -> "smear", "c" -> 5|>,
   <|"a" -> 5, "b" -> "year", "c" -> 7|>,
   <|"a" -> 6, "b" -> "ear", "c" -> 12.4335|>}]


\begin{array}{ccc} 1.00,xman,1.00\\ 2.00,bear,3.14\\ 3.00,fear,3.00\\ 4.00,smear,5.00\\ 5.00,year,7.00\\ 6.00,ear,12.43\\ \end{array}

in the exported file

Update 1

The code update that does not strip the string from quotation marks

tonum[x_?NumericQ] := NumberForm[x, {Infinity, 2}]
tonum[x_] := x
 Map[tonum, ds[Values] // Normal, {2}]]

and the output is


\begin{array}{ccc} 1.00,"xman",1.00\\ 2.00,"bear",3.14\\ 3.00,"fear",3.00\\ 4.00,"smear",5.00\\ 5.00,"year",7.00\\ 6.00,"ear",12.43\\ \end{array}

Update 2

It appears that the Export strips double quotes when the string is Wrapped with NumberForm. Number form has no effect when displaying string the in front end. I do not know if this is intended behavior of export or a bug.

| improve this answer | |
  • $\begingroup$ There are also some nice examples here $\endgroup$ – ercegovac Oct 3 '17 at 11:46
  • $\begingroup$ Just to get a perfect CSV file. How to avoid having the {....} in the output file. Thanks. $\endgroup$ – Porty Oct 4 '17 at 8:02
  • $\begingroup$ @Porty The code is now updated ... If the answer solves your problem accept it so that it gets removed from unanswered queue. $\endgroup$ – ercegovac Oct 4 '17 at 8:23
  • $\begingroup$ @Porty Following the first sentence of your original question, I have tailored the answer according to my understanding of "string" and "numbers". IMO, the code does exactly that. However, if the word "number" had some different meaning, you should have included a working example and expected result in your original question. Hence, before continuing this chase, please clarify where the parenthesis come from? You did not mention that your dataset contains lists of numbers. $\endgroup$ – ercegovac Oct 4 '17 at 8:48

Really sorry for my english, I was speaking about the question marks of strings, not the parenthesis. Based on your help, this works :

| improve this answer | |
  • $\begingroup$ Does the latest update solve your problem? $\endgroup$ – ercegovac Oct 4 '17 at 9:57
  • $\begingroup$ Yes. Thanks a lot. $\endgroup$ – Porty Oct 5 '17 at 9:22

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.