Group Abstract Group Abstract

Message Boards Message Boards

1
|
9.4K Views
|
4 Replies
|
1 Total Like
View groups...
Share
Share this post:

Extracting values from a Dataset?

Posted 9 years ago

Given a hierarchical data set, I am looking for a way to extract a list of values by their keys. When I use the Part syntax for a single key, I get the value. But when I use a list of keys, I get a Dataset. Is there a succinct way to get a list of values instead?

In[1]:= as = Association /@ {
    {"a" -> 1, "b" -> 2, "c" -> 3},
    {"d" -> 2, "e" -> 3, "f" -> 1}
    };

In[2]:= ds = Dataset[as]

Out[2]= Dataset[{__Association}]

In[3]:= ds[[1, "b"]]

Out[3]= 2

In[4]:= ds[[1, {"b", "c"}]]

Out[4]= Dataset[_Association]

What I want is {2,3}
POSTED BY: David Keith
4 Replies

I'm not quite certain when Dataset should be used instead of Association, or when they should be mixed. But what about this:

as = Association /@ {{"a" -> 1, "b" -> 2, "c" -> 3}, {"d" -> 2, "e" -> 3, "f" -> 1}};
{"b", "c", "f"} /. Association[as]

{2, 3, 1}
Posted 9 years ago

And thanks, David. I also am unsure as to when to use structured lists and when to use associations or datasets. In this case I like the dataset just because of its default display form. I am really processing ray files from an optical simulation. The files consist of descriptions of rays, where each ray is defined by a sequence of ray segments, with 31 items of interest for each segment, and it is these that benefit from descriptive names. So a single ray can be extracted from the database and seen as a list of segments, with a header representing identifying the segment data. And the segment detials can be extracted by keys. I also suspect I will have use for the Query capabilities.

enter image description here

Best,

David

POSTED BY: David Keith
Posted 9 years ago

Just convert into normal expression and get the values-

Values[Normal[ds[1, {"b", "c"}]]]
POSTED BY: Girish Arabale
Posted 9 years ago

Thanks, Girish -- that works great.

POSTED BY: David Keith
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard