Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.math.mathematica

Topic: Extract columns based on their the title in header
Replies: 6   Last Post: Oct 20, 2013 11:01 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Bob Hanlon

Posts: 891
Registered: 10/29/11
Re: Extract columns based on their the title in header
Posted: Sep 21, 2013 4:34 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply


dataList = {
{"Name", "Sex", "Age"},
{"Tom", "M", 12},
{"Bob", "M", 13},
{"Lily", "F", 11},
{"Jack", "M", 14},
{"Michael", "M", 10}
};


Cases[dataList // Transpose,
{"Name" | "Age", ___}] // Transpose


{{"Name", "Age"}, {"Tom", 12}, {"Bob", 13}, {"Lily", 11}, {"Jack",
14}, {"Michael", 10}}


(dataList // Transpose)[[
Position[dataList[[1]], "Name" | "Age"] //
Flatten]] // Transpose


{{"Name", "Age"}, {"Tom", 12}, {"Bob", 13}, {"Lily", 11}, {"Jack",
14}, {"Michael", 10}}



Bob Hanlon




On Fri, Sep 20, 2013 at 7:07 AM, Joug Raw <jougraw@gmail.com> wrote:

>
> I import a CSV file like this:
>
> Name, Sex, Age, ... , ...
> Tom, M, 12, ... ,...
> Bob, M, 13,
> Lily, F, 11,
> Jack, M, 14,
> Micheal, M, 10,
> ..
>
> It become a list and I call it datalist. Then I want only extract the Name
> and Age for each child in the list. I did some thing like:
>
> ColumPos =
> Map[#[[2]] &,
> Map[Position[datalist, #] &, {"Name",
> "Age"}], {2}]
> Map[Extract[#, ColumPos] &, datalist]
>
> Yes, it works. But the code are bit complicated. Any one has any idea to
> get it done in a more efficient way?
>
>
>




Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.