2
$\begingroup$

I would like to select columns from a table by column name. I tried the solution described here: Data Table Manipulation in Mathematica: Step 2 . I am using Mathematica 10.

(I would write a comment there, instead of a new question, but StackExchange doesn't let me write comments yet.)

$\endgroup$
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – user9660 Mar 19 '15 at 7:25
  • $\begingroup$ look at Association in mathematica's documentation $\endgroup$ – k_v Mar 19 '15 at 7:36
  • $\begingroup$ Ah! I realized that I couldn't use the solution described in (mathematica.stackexchange.com/questions/29336/…) because my data was in TableForm. $\endgroup$ – Ceilican Mar 19 '15 at 7:38
  • $\begingroup$ Thanks for pointing Association, @k_v! $\endgroup$ – Ceilican Mar 19 '15 at 7:47
3
$\begingroup$

Whilst valid Leonid's code has been superseded by new functionality in Mathematica 10. As well as Association take a look at the Dataset, Select and Query functions in the help. But basically try some of the following :

data = {{"1", "Cat", "Tiddles"}, {"2", "Dog", "Spot"}};

cols = {"id", "animal", "name"};

(* turns a list of animals into an association and then into a 
 dataset with column headings specified by cols *)

animalsDS = Dataset[AssociationThread[cols -> #] & /@ data]

full dataset

(* Just select cats *)

animalsDS[Select[#animal == "Cat" &]]

just cat

(* select cats with wildcards *)

animalsDS[Select[StringMatchQ[#"animal", "Ca*" ] &]]

(* selects animals that are cats AND (&&) called tiddles *)

animalsDS[Select[#animal == "Cat" && #name == "Tiddles" &]]

(* selects animals that are cats OR (||) dogs *)

animalsDS[Select[#animal == "Cat" ||  #animal == "Dog" &]]
$\endgroup$

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.