Quantcast
Channel: How to work with a Dataset? - Mathematica Stack Exchange
Viewing all articles
Browse latest Browse all 3

How to work with a Dataset?

$
0
0

I have an application for which a Dataset appears useful. I want a structured dataset which consists of a set of records, which are presumably Associations.

For the data set, I would like to be able to perform the following operations:

  1. Add a record
  2. Add one or more Key-Value pairs to a given record.
  3. Modify the value associated with a key in a given record
  4. Perform functions on the dataset, such as extracting records for which a particular key has a specified value, or total all keys for a set of records for which a different key has a specified value.

I generally want to perform these operations in place.

I have read and re-read the docs on this, and I am still confused. In the code below you can see some attempts. (I deleted the output because the grid forms don't usually look good in a posting.)

First, I construct a dataset in two different ways. The first way is to create an empty dataset and append records to it. The second way is to construct the dataset immediately containing the records. These two constructs have the same Normal, but they are displayed differently.

In the second section, I try to add key-value pairs to one of the records. My method does not work.

I am also not sure whether a Dataset or a list of Associations would be best.

Anyone who could point me in the right direction for this would be greatly appreciated!

db=Dataset[{}]AppendTo[db,<|"a"->1,"b"->2|>]AppendTo[db,<|"a"->2,"b"->5|>]db//Normal(* create it with the associations *)db2=Dataset[{<|"a"->1,"b"->2|>,<|"a"->2,"b"->5|>}](* Why are they displayed differently, but have the same Normal form? *)db2//Normal(* try to add a key to the first record *)(* db2 has part 1 *)db2[[1]](* but not here *)AssociateTo[db2[[1]],"c"->7]db2[1]AssociateTo[db2[1],"c"->7]

Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images