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.


1 Answer 1



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.

  • $\begingroup$ 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.... $\endgroup$
    – ramesh
    Aug 12, 2014 at 0:54
  • $\begingroup$ @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. $\endgroup$ Aug 12, 2014 at 2:33
  • 4
    $\begingroup$ @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/tests/data/iris.csv"] $\endgroup$ Aug 12, 2014 at 2:36
  • 2
    $\begingroup$ An alternative way to create a Dataset is irisDataset = Dataset[Inner[Rule, header, Transpose[data], Association]]. $\endgroup$ Aug 12, 2014 at 7:38
  • 3
    $\begingroup$ @PhilChang You can achieve the same result as in your answer with just Import with the Dataset representation and the HeaderLines option: Import["d:/iris.csv", {"CSV", "Dataset"}, "HeaderLines" -> 1] $\endgroup$ Nov 24, 2017 at 0:42

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.