Excerpt |
---|
This section describes how to unnest the values in an Array into separate columns in your dataset. |
Source:
In the following example dataset, students took the same test three times, and their scores were stored in any array in the Scores
column.
LastName | FirstName | Scores |
---|
Adams | Allen | [81,87,83,79] |
Burns | Bonnie | [98,94,92,85] |
Cannon | Chris | [88,81,85,78] |
Transformation:
When the data is imported, you might have to re-type the Scores
column as an array:
D trans |
---|
RawWrangle | true |
---|
Type | step |
---|
WrangleText | settype col: Scores type: 'Array' |
---|
p01Name | Columns |
---|
p01Value | Scores |
---|
p02Name | New type |
---|
p02Value | Array |
---|
SearchTerm | Change column data type |
---|
|
You can now unnest the Scores
column data into separate columns:
D trans |
---|
RawWrangle | true |
---|
p03Value | [1] |
---|
p06Name | Remove elements from original |
---|
WrangleText | flatten col: Scores |
---|
p01Name | Column |
---|
p06Value | true |
---|
p03Name | Parameter: Paths to elements |
---|
p07Value | true |
---|
p04Value | [2] |
---|
SearchTerm | Unnest Objects into columns |
---|
p07Name | Include original column name |
---|
Type | step |
---|
p05Name | Parameter: Paths to elements |
---|
p01Value | Scores |
---|
p02Name | Parameter: Paths to elements |
---|
p02Value | [0] |
---|
p05Value | [3] |
---|
p04Name | Parameter: Paths to elements |
---|
|
In the above transformation:
- Each path is specified in a separate row.
- The
[x]
syntax indicates that the path is the xth element of the array. - The first element of an array is referenced using
[0]
.
- You can choose to delete the element from the original or not. Deleting the element can be a helpful way of debugging your transformation. If all of the elements are gone, then the transformation is complete.
- If you include the original column name in the output column names, you have some contextual information for the outputs.
Results:
LastName | FirstName | Scores_0 | Scores_1 | Scores_2 | Scores_3 |
---|
Adams | Allen | 81 | 87 | 83 | 79 |
Burns | Bonnie | 98 | 94 | 92 | 85 |
Cannon | Chris | 88 | 81 | 85 | 78 |