Message Boards Message Boards

1
|
3490 Views
|
10 Replies
|
10 Total Likes
View groups...
Share
Share this post:

How to add headers to a dataset?

Posted 11 months ago

Suppose I have the following dataset:

dataset = Dataset[{{195, 2.3}, {198, 6.4}, {204, -3.1}}]

How can I add a header x to the first column and a header y to the second column, so to get the following new dataset:?

Dataset[{
  <|"x" -> 195, "y" -> 2.3|>,
  <|"x" -> 198, "y" -> 6.4|>,
  <|"x" -> 204, "y" -> -3.1|>
  }]

I am also looking for a command to assing units (cm and volts) to the values, so to get the following:

Dataset[{
  <|"x" -> Quantity[195, "Centimeters"], "y" -> Quantity[2.3, "Volts"]|>,
  <|"x" -> Quantity[198, "Centimeters"], "y" -> Quantity[6.4, "Volts"]|>,
  <|"x" -> Quantity[204, "Centimeters"], "y" -> Quantity[-3.1, "Volts"]|>
  }]

Any ideas?

POSTED BY: Ehud Behar
10 Replies

POSTED BY: Vitaliy Kaurov
Posted 11 months ago

Are you required to start with

dataset = Dataset[{{195, 2.3}, {198, 6.4}, {204, -3.1}}]

or do you have access to the raw data directly? E.g.

data = {{195, 2.3}, {198, 6.4}, {204, -3.1}}
POSTED BY: Eric Rimbey
Posted 11 months ago

Here's one way to start...

dataset = Dataset[{{195, 2.3}, {198, 6.4}, {204, -3.1}}];
datasetWithHeader = dataset[All, Association @@ Thread[{"x", "y"} -> #] &]

enter image description here

Then you could continue:

datasetWithHeader[All, <|"x" -> Quantity[#x, "Centimeters"], "y" -> Quantity[#y, "Volts"]|> &]

enter image description here

POSTED BY: Eric Rimbey
Posted 11 months ago

For your second question, on adding units:

dataset = Dataset[{{195, 2.3}, {198, 6.4}, {204, -3.1}}]
Map[{Quantity[#[[1]],"Centimeters"],Quantity[#[[2]],"Volts"]}&,dataset//Normal]//Dataset
POSTED BY: Hans Milton
Posted 11 months ago

Then I might do something like this:

Dataset[MapApply[<|"x" -> Quantity[#1, "Centimeters"], "y" -> Quantity[#2, "Volts"]|> &, rawData]]
POSTED BY: Eric Rimbey
Posted 11 months ago

I have access to the raw data, and I would like to see an additional approach if I start with "data".

POSTED BY: Ehud Behar
Posted 11 months ago

Very nice

POSTED BY: Ehud Behar
Posted 11 months ago

Thanks very much.

Regarding this instruction you wrote:

dataset[All,Key["c"]/*<|"ctotal"->Total,"clength"->Length|>] 

Can you show me how to write it in a "FullForm" notatation, With explicit RightComposition? I am not so familiar with this infix shortcut and don't fully understand which expression is the argument of which function.

Another question: You showed how to apply a function (Total and Length) on the values in each row.

Suppose I want to add 3 to each value, or multiply each value by 2, in column a, how to do it?

POSTED BY: Ehud Behar

Using a resource function...

dataset = Dataset[{{195, 2.3}, {198, 6.4}, {204, -3.1}}];

ResourceFunction["DatasetWithHeaders"][ dataset, {"x", "y"}]
POSTED BY: Joshua Schrier
Posted 1 month ago

Another way, if you start with raw numbers:

list = {{195, 2.3}, {198, 6.4}, {204, -3.1}}

Note that both Thread[{"x", "y"} -> {195, 2.3}] and Thread[{"x", "y"} -> #] &[{195, 2.3}] return

{"x" -> 195, "y" -> 2.3}

Map the pure function Thread[{"x", "y"}->#]& on every sub-list:

Map[Thread[{"x", "y"} -> #] &, list]
(*{{"x" -> 195, "y" -> 2.3}, {"x" -> 198, "y" -> 6.4}, {"x" -> 204, "y" -> -3.1}}*)

and replace the head of each sub-list with that of Association:

Apply[Association, Map[Thread[{"x", "y"} -> #] &, list], {1}]
(*or*)
Association @@@ Map[Thread[{"x", "y"} -> #] &, list]
(* {<|"x" -> 195, "y" -> 2.3|>, <|"x" -> 198, "y" -> 6.4|>, <|"x" -> 204, "y" -> -3.1|>} *)

Only thing left is to make this a Dataset:

Dataset[%]

Written as a single instruction:

Association @@@ Map[Thread[{"x", "y"} -> #] &, list] // Dataset
POSTED BY: Ehud Behar
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract