Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

It's old question. However, I could not find satisfactory answer. Decided to post this question. I have a .csv file with column heading. How can I import this .csv file into Mathematica assigning column names as list names in short and sweet way. For example, I can use the following codes.

mydata = Import[file, Path -> path];
names = Flatten[mydata[[;; 1]]]; 
Do[listnames[names[[i]]] = 
                     mydata[[2 ;;]][[All, i]], {i, 1, Dimensions[mydata[[2 ;;]]][[2]]}];

But I need to write listnames["Col names in .csv file"] to refer to each list. I can not refer .csv column names as list names. I need to figure out this because I have over 1000 column headings in my .csv data. Please help me to figure out his without long codes.

share|improve this question

1 Answer 1

up vote 6 down vote accepted

Update

Inspired by Andy Ross

header = First @ iris;

header = StringReplace[header,"."->""](Because Dot is protected in Mathematica)

data = Rest @ iris;

assign[name_, value_] := Evaluate[ToExpression[name]] = value;

Thread[ assign[header, Transpose@data] ]

====================================================================

If you are using Mathematica 10, there is a new data type called Dataset. Take the iris data for example. Suppose the iris.csv is in d:\.

iris = Import["d:/iris.csv","CSV"];

header = iris[[1]];

data = iris[[2;;]];

irisDataset = Thread[header -> #]& /@ data // Map[Association] // Dataset

and it will display like this:

enter image description here

Please refer to Dataset reference for more information.

share|improve this answer
    
Thank you for your time. Yes, I am using Mathematica 10. I tried your codes but it does not give me the results I desire. I want the column names of .csv file as list names so that I can refer each .csv column data by their column name as list name in Mathematica. In your example, I should be able to refer each column as a separate list with its name being column heading. I should be able to refer first column as a list as Sepal.Length, second column as another list as Sepal.Width and so on.... –  rka Aug 12 at 0:54
    
@rkadhikari I advise reading the documentation page for Dataset -- it's pretty trivial to take a dataset and extract the raw columns from it, but you might find the Dataset itself much easier to work with for some kinds of things. –  Taliesin Beynon Aug 12 at 2:33
    
Thanks for your time, PhilChang. That's what I was looking for. You are really genius!!:) –  rka Aug 12 at 2:34
1  
@PhilChang SemanticImport makes it really easy to import a tabular file as a Dataset if they have a reasonable structure -- no need to parse it to and format it yourself. So for example your code could just be replaced with SemanticImport["https://raw.githubusercontent.com/pydata/pandas/master/pandas/t‌​ests/data/iris.csv"] –  Taliesin Beynon Aug 12 at 2:36
1  
An alternative way to create a Dataset is irisDataset = Dataset[Inner[Rule, header, Transpose[data], Association]]. –  Ernst Stelzer Aug 12 at 7:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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