0
$\begingroup$

I am trying to import a CSV file with the following structure:

  • the 1st column has 15 values,
  • the 2nd column has 10 values,
  • and 3rd column has 15 values.

But when I import and try to access the 2nd column, it shows 5 commas (, , , , ,)

Clear["Global`*"]
a1 = Import["data.csv", {"Data", All, All}]

so I get the following output:

{{"", 1, 2}, {1, 24.479, 24.4857}, {2, 24.6406, 24.5881}, {3, 24.8059,
   24.8043}, {4, 24.9217, 24.9197}, {5, 25.0553, 24.9319}, {6, 
  25.1328, 25.0986}, {7, 25.2248, 25.2658}, {8, 25.3725, 25.3393}, {9,
   25.4888, 25.4905}, {10, 25.6224, 25.6009}, {11, "", 25.7327}, {12, 
  "", 25.8424}, {13, "", 25.9748}, {14, "", 26.1249}, {15, "", 26.1774}}

and when I access the 2nd column,

ao = a1[[All, 2]]

I get the following

{1, 24.479, 24.6406, 24.8059, 24.9217, 25.0553, 25.1328, 25.2248,   
25.3725, 25.4888, 25.6224, "", "", "", "", ""}

Is there any way to get rid of commas/spaces without doing it manually?

$\endgroup$
7
  • 1
    $\begingroup$ it will help if you put a link to your .csv file so one can try. What happens if you do Import[ "data.csv","Data"] ? $\endgroup$
    – Nasser
    Commented Feb 9 at 10:54
  • $\begingroup$ @Nasser thanks edited. $\endgroup$
    – a019
    Commented Feb 9 at 11:11
  • $\begingroup$ I meant a link to the .csv file itself. So one can download it and try. It seems some fields are empty in your file. $\endgroup$
    – Nasser
    Commented Feb 9 at 11:17
  • 1
    $\begingroup$ Use dta /. "" -> Nothing, where dta is your data file. $\endgroup$
    – bbgodfrey
    Commented Feb 9 at 11:53
  • 1
    $\begingroup$ CSV import has an "EmptyFields" option that lets you control how these are handled. For example: Import["data.csv", "EmptyFields"->Missing[]] reference.wolfram.com/language/ref/format/CSV.html $\endgroup$ Commented Feb 9 at 13:40

1 Answer 1

0
$\begingroup$

will

ao/.""->Sequence[]

work?

Or may be better to do a1 /. "" -> 0, as it will keep the structure of your table.

$\endgroup$
2
  • 1
    $\begingroup$ Please include an example of how your solutions work on the list in the question. $\endgroup$
    – bbgodfrey
    Commented Feb 9 at 11:54
  • 1
    $\begingroup$ I do not think replacing empty field with zero is good idea. Because when looking at the data inside Mathematica, how will now one know if the zero was from the original file (i.e. original data) or it was introduced manually later on? $\endgroup$
    – Nasser
    Commented Feb 9 at 11:56

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.