Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mathematica - Import CSV and process columns?

I have a CSV file that is formatted like:

0.0023709,8.5752e-007,4.847e-008

and I would like to import it into Mathematica and then have each column separated into a list so I can do some math on the selected column.

I know I can import the data with:

Import["data.csv"]

then I can separate the columns with this:

StringSplit[data[[1, 1]], ","]

which gives:

{"0.0023709", "8.5752e-007", "4.847e-008"}

The problem now is that I don't know how to get the data into individual lists and also Mathematica does not accept scientific notation in the form 8.5e-007.

Any help in how to break the data into columns and format the scientific notation would be great.

Thanks in advance.

like image 777
Nope Avatar asked Mar 26 '10 17:03

Nope


3 Answers

KennyTM is correct.

data = Import["data.csv", "CSV"];
column1 = data[[All,1]]
column2 = data[[All,2]]
...
like image 60
Davorak Avatar answered Sep 30 '22 02:09

Davorak


Davorak's answer is the correct one if you need to import a whole CSV file as an array. However, if you have a single string that you need to convert from the C/Fortran-style exponential notation, you can use ImportString with different arguments for the format. As an example, there's

In[1]:= ImportString["1.0e6", "List"]
Out[1]= {1.*^6}

The *^ operator is Mathematica's equivalent of the e. Note this is also a good way to split apart strings that are in CSV form:

In[2]:= ImportString["1.0e6,3.2,foo", "CSV"] 
Out[2]= {{1.*10^6,3.2,foo}}

In both cases, you'll get your answer wrapped up in an extra level of list structure, which is pretty easy to deal with. However, if you're really sure you only have or want a single number, you can turn the string into a stream and use Read. It's cumbersome enough that I'd stick to ImportString, however:

In[3]:= Module[{stream = StringToStream["1.0e6"], number},
          number = Read[stream, "Number"];
          Close[stream];
          number]
Out[3]= 1.*10^6
like image 24
Pillsy Avatar answered Sep 30 '22 02:09

Pillsy


You can fix the notation by using StringReplace[].

In[1]: aa = {"0.0023709", "8.5752e-007", "4.847e-008"};

In[2]: ToExpression[
          StringReplace[
             #,
             RegularExpression@"(^\d+\.\d+)e([+-]\d+)" -> "$1*10^$2"
          ]
       ] & @ aa

Out[2]: {0.0023709, 8.5752*10^-7, 4.847*10^-8}

You can put the entire data array in place of aa to process is all at once with a one liner

{col1,col2,col3} = ToExpression[...] & @ Transpose[Import["data.csv", "CSV"]];

with ToExpression[...] as above.

like image 26
Timo Avatar answered Sep 30 '22 02:09

Timo