Dataset

Dataset[data]
represents a structured dataset based on a hierarchy of lists and associations.

DetailsDetails

  • Dataset can represent not only full rectangular multidimensional arrays of data, but also arbitrary tree structures, corresponding to data with arbitrary hierarchical structure.
  • Dataset[][op1,op2,] is equivalent to Query[op1,op2,][Dataset[]], which applies the sequence of operators at successively deeper levels and yields the resulting Dataset object.
  • The can be any of the following forms:
  • All,i,i;;j,"key",Key[]part operators
    Select[],MaximalBy[],filtering operators
    Counts,Total,Mean,Max,aggregation operators
    Query[],{op1,op2,},subquery operators
    Function[],farbitrary functions
  • In Dataset[][op1,op2,], the are applied at successively deeper levels in expr, but any given one may be applied either while "descending" into expr or while "ascending" out of it. In general, part specifications and filtering operators are "descending" operators. Aggregation operators, subquery operators, and arbitrary functions are "ascending" operators.
  • A "descending" operator is applied to corresponding parts of the original dataset, before subsequent operators are applied at deeper levels. Descending operators have the feature that they do not change the structure of deeper levels of the data when applied at a certain level. This ensures that subsequent operators will encounter subexpressions whose structure is identical to the corresponding levels of the original dataset. The simplest descending operator is All, which selects all parts at a given level and therefore leaves the structure of the data at that level unchanged.
  • An "ascending" operator is applied after all subsequent operators have been applied to deeper levels. Whereas descending operators correspond to the levels of the original data, ascending operators correspond to the levels of the result. Unlike descending operators, ascending operators do not necessarily preserve the structure of the data they operate on. Unless an operator is specifically recognized to be descending, it is assumed to be ascending.
  • The "descending" part operators specify which elements to take at a level before applying any subsequent operators to deeper levels:
  • Allapply subsequent operators to each part of a list or association
    i;;jtake parts i through j and apply subsequent operators to each part
    itake only part i and apply subsequent operators to it
    "key",Key[key]take value of key in an association and apply subsequent operators to it
    Keystake keys of an association and apply subsequent operators to each key
    Valuestake values of an association and apply subsequent operators to each value
    {part1,part2,}take given parts and apply subsequent operators to each part
  • The "descending" filtering operators specify how to rearrange or filter elements at a level before applying subsequent operators to deeper levels:
  • Select[test]take only those parts of a list or association that satisfy test
    SelectFirst[test]take the first part that satisfies test
    KeySelect[test]take those parts of an association whose keys satisfy test
    MaximalBy[crit],MinimalBy[crit]take the parts for which criteria crit is minimal or maximal
    SortBy[crit]sort parts in order of crit
    KeySortBy[crit]sort parts of an association based on their keys, in order of crit
    DeleteDuplicatesBy[crit]take parts that are unique according to crit
    DeleteMissingdrop elements with head Missing
  • The "ascending" aggregation operators combine or summarize the results of applying subsequent operators to deeper levels:
  • Totaltotal all quantities in the result
    Min,Maxgive minimum, maximum quantity in the result
    Mean,Median,Quantile,give statistical summary of the result
    Histogram,ListPlot,calculate a visualization on the result
    Merge[f]merge common keys of associations in the result using function f
    Catenatecatenate the elements of lists or associations together
    Countsgive association that counts occurences of values in the result
    CountsBy[crit]give association that counts occurences of values according to crit
    CountDistinctgive number of distinct values in the result
    CountDistinctBy[crit]give number of distinct values in the result according to crit
  • The "ascending" subquery operators perform a subquery after applying subsequent operators to deeper levels:
  • Query[]perform a subquery on the result
    {op1,op2,}apply multiple operators at once to the result, yielding a list
    op1/* op2/* apply , then apply at the same level, etc.
    <|key1op1,key2op2,|>apply multiple operators at once to the result, yielding an association with the given keys
    {key1op1,key2op2,}apply different operators to specific parts in the result
  • When one or more descending operators are composed with one or more ascending operators (e.g. ), the descending part will be applied, then subsequent operators will be applied to deeper levels, and lastly the ascending part will be applied to the result.
  • The special descending operator GroupBy[spec] will introduce a new association at the level at which it appears, and can be inserted or removed from an existing query without affecting the behavior of other operators.
  • Where possible, type inference is used to determine whether operations will succeed. Operations that are guaranteed to fail will result in a Failure object being generated.
  • When a Dataset operation returns structured data (e.g. a list or association or nested combinations of these), the result will be given in the form of another Dataset object. Otherwise, the result will be given as an ordinary Wolfram Language expression.
  • Normal can be used to convert any Dataset object to a combination of lists and associations.

ExamplesExamplesopen allclose all

Basic Examples  (1)Basic Examples  (1)

Create a Dataset object from tabular data:

In[1]:=
Click for copyable input
Out[1]=

Take a set of rows:

In[2]:=
Click for copyable input
Out[2]=

Take a specific row:

In[3]:=
Click for copyable input
Out[3]=

A row is merely an association:

In[4]:=
Click for copyable input
Out[4]=

Take a specific element from a specific row:

In[5]:=
Click for copyable input
Out[5]=
In[6]:=
Click for copyable input
Out[6]=
In[7]:=
Click for copyable input
Out[7]=

Take the contents of a specific column:

In[8]:=
Click for copyable input
Out[8]=
In[9]:=
Click for copyable input
Out[9]=
In[10]:=
Click for copyable input
Out[10]=

Take a specific part within a column:

In[11]:=
Click for copyable input
Out[11]=

Take a subset of the rows and columns:

In[12]:=
Click for copyable input
Out[12]=

Apply a function to the contents of a specific column:

In[13]:=
Click for copyable input
Out[13]=
In[14]:=
Click for copyable input
Out[14]=
In[15]:=
Click for copyable input
Out[15]=

Partition the dataset based on a column, applying further operators to each group:

In[16]:=
Click for copyable input
Out[16]=

Apply a function to each row:

In[17]:=
Click for copyable input
Out[17]=

Apply a function both to each row and to the entire result:

In[18]:=
Click for copyable input
Out[18]=

Apply a function to every element in every row:

In[19]:=
Click for copyable input
Out[19]=

Apply functions to each column independently:

In[20]:=
Click for copyable input
Out[20]=

Construct a new table by specifying operators that will compute each column:

In[21]:=
Click for copyable input
Out[21]=

Use the same technique to rename columns:

In[22]:=
Click for copyable input
Out[22]=

Select specific rows based on a criterion:

In[23]:=
Click for copyable input
Out[23]=

Take the contents of a column after selecting the rows:

In[24]:=
Click for copyable input
Out[24]=

Take a subset of the available columns after selecting the rows:

In[25]:=
Click for copyable input
Out[25]=

Take the first row satisfying a criterion:

In[26]:=
Click for copyable input
Out[26]=

Take a value from this row:

In[27]:=
Click for copyable input
Out[27]=

Sort the rows by a criterion:

In[28]:=
Click for copyable input
Out[28]=

Take the rows that give the maximal value of a scoring function:

In[29]:=
Click for copyable input
Out[29]=

Delete rows that duplicate a criterion:

In[30]:=
Click for copyable input
Out[30]=
In[31]:=
Click for copyable input
Out[31]=

Compose an ascending and a descending operator to aggregate values of a column after filtering the rows:

In[32]:=
Click for copyable input
Out[32]=

Do the same thing by applying Total after the query:

In[33]:=
Click for copyable input
Out[33]=
Introduced in 2014
(10.0)