6
$\begingroup$

A dataset can be created in at least two ways: first, from a list of associations, second, from an association of associations (I know that it is also possible to create it as a list of list and an association of lists, but these last two cases are not relevant).

1. This is a list of associations.

ds = Dataset@{
    <|"nombre" -> "Aileen Agüero García", "edad" -> 25,"licenciatura" -> "Economía", 
     "universidad" -> "PUCP"|>, 
    <|"nombre" -> "Alvaro Gálvez Pasco","edad" -> 45, "licenciatura" -> "Ciencia política", 
     "universidad" -> "PUCP"|>,
    <|"nombre" -> "Gastón Zapata ", "edad" -> 55,"licenciatura" -> "Antropología", 
    "universidad" -> "PUCP"|>,
    <|"nombre" -> "Jose Velasco", "edad" -> 55,"licenciatura" -> "Economía", "universidad"-> 
    "PUCP"|>
    };

and the dataset is:

ds

If I want to add a column I do the following:

(* Agregar una columna con distintos valores a un dataset *)
col1 = {1, 2, 3, 4}; (*se crea una lista de valores*)
nuevo = Dataset[AssociationThread["ID" -> #] & /@col1];
Join[nuevo, ds, 2] (*finalmente se unen los dos datasets usando el nivel 
de especificación 2*)

And the output is:

ds2

2. Now, if I have an association of associations:

baseDeDatos = <|
  "Registro1" -> <|"Nombre" -> "Juan", "Edad" -> 25,"Ciudad" -> "Madrid"|>, 
  "Registro2" -> <|"Nombre" -> "Maria", "Edad" -> 30,"Ciudad" -> "Barcelona"|>, 
  "Registro3" -> <|"Nombre" -> "Pedro", "Edad" -> 22,"Ciudad" -> "Valencia"|>|>

ds3

I don't know how to add a column to it. For example, I have tried with the following code:

ds2[All, Append[{"Estado civil" -> "Soltero", 
   "Estado civil" -> "Casado", "Estado civil" -> "Viudo"}]]

But there is an error in the output. Please help me!

ds4

$\endgroup$

3 Answers 3

5
$\begingroup$
ds[All, 
   Module[{$c = 0}, <|"ID" -> ++$c, #|> &]]

enter image description here

ds2[All, 
 Module[{$c = 0}, 
   <|"ID" -> ++$c, #, 
     "Estado civil" -> {"Soltero", "Casado", "Viudo"}[[$c]]|> &]]

enter image description here

$\endgroup$
4
$\begingroup$

You can add items using Prepend/Append like e.g. to add an ID number:

c = 1;
ds = Prepend[#, ID -> c++] & /@ baseDeDatos

enter image description here

Note that MMA now displays the data set column wise, because we have one 4 items:

enter image description here

$\endgroup$
4
$\begingroup$
base =
  <|"Registro1" -> <|"Nombre" -> "Juan", "Edad" -> 25, "Ciudad" -> "Madrid"|>, 
   "Registro2" -> <|"Nombre" -> "Maria", "Edad" -> 30, "Ciudad" -> "Barcelona"|>, 
   "Registro3" -> <|"Nombre" -> "Pedro", "Edad" -> 22, "Ciudad" -> "Valencia"|>|>;

nuevo =
  {"Estado civil" -> "Soltero", "Estado civil" -> "Casado", "Estado civil" -> "Viudo"};

AssociationThread[Keys @ base, 
  MapThread[Append, {Values @ base, nuevo}]] // Dataset

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.