Given example tabular data (ie, with rows and columns, or "data frames")
data = Table[<| "key1" -> i, "key2" -> i + 10 ,
"key3" -> i + 20|>, {i, 4}] //Dataset
The following query operator almost works to associate (the values of) a selected "primary" key at first level:
primaryKey[key_] := Sequence[All, Slot[key] -> KeyDrop[#, key] &]
This allows the lookup syntax:
data[primaryKey["key1"]][3]
(* 3 -> <|"key2" -> 13, "key3" -> 23|> *)
How to modify this query to handle:
Return only the value
<|"key2" -> 13, "key3" -> 23|>
, similar to howdata[All, #key1 &] // Normal
returns only the values{1, 2, 3, 4}
.Dataset doesn't recognize the implicit global association structure, flattening the data by 1 level:
data[primaryKey["key1"]]
The following workaround works but how to incorporate in the operator?
data[primaryKey["key1"]] // Normal // Association // Dataset
The same issue arises from other restructuring operators, like generating an association from (the values of) two specified keys eg
data[All, #key1 -> #key3 &][2]
(* 2 -> 22 *)
Whereas
data[All, #key1 -> #key3 &] // Normal // Association //
Dataset // #[2] &
(* 22 *)
Answer
Changing the descending operator All
to the ascending Association
seems to work at turning the resulting list of rules into an association.
primaryKey[key_] := Sequence[Association, Slot[key] -> KeyDrop[#, key] &]
Comments
Post a Comment