Extractlist Transform

Extracts a set of values based on a specified pattern from a source column of any data type. The generated column contains an array of occurrences of the specified pattern.While the new column contains array data, the data type of the new column is sometimes inferred as String.

Basic Usage

Your source column (myWidgetInventory) is formatted in the following manner:

{ "red":"100","white":"1300","blue":"315","purple":"55"}

The following transform extracts the raw inventory contents of each color:

extractlist col: myWidgetInventory on:`{digit}+`

Output: The generated column contains data that looks like the following array:

["100","1300","315","55"]

Parameters

extractlist: col:column_ref on:string_literal_pattern delimiter:string_literal_pattern [quote:'quoted_string'] [as:'new_column_name']

TokenRequired?Transform BuilderData TypeDescription
extractlistYConvert list to ArraytransformName of the transform
colYColumnstringSource column name
onYOn patternstringString literal or pattern that identifies the values to extract from the source column
delimiterYDelimiterstringString literal or pattern that identifies the separator between the values to extract
quoteNIgnore delimiters betweenstringSpecifies a quoted object that is omitted from matching delimiters
asNNew column namestringName of the newly generated column

For more information on syntax standards, see Language Documentation Syntax Notes.

col

Identifies the column to which to apply the transform. You can specify only one column.

Usage Notes:

Required?Data Type
YesString (column name)

on

Identifies the pattern to match. Pattern can be a string literal, Cloud Dataprep pattern, or regular expression pattern.

Tip: You can insert the Unicode equivalent character for this parameter value using a regular expression of the form /\uHHHH/. For example, /\u0013/ represents Unicode character 0013 (carriage return). For more information, see Supported Special Regular Expression Characters.

For the extractlist tranform, all instances that match this pattern in the source column are extracted into the array list in the new column. Each occurrence in the generated array corresponds to an individual instance in the source; the new column can contain duplicate values.

To create array elements based only on the delimiter parameter, set the following regular expression:

on:`/+/`

Usage Notes:

Required?Data Type
Yes

String (literal, regular expression, or Cloud Dataprep pattern )

delimiter

Specifies the character or pattern that defines the end of a key-value pair. This value can be specified as a String literal, regular expression, or Cloud Dataprep pattern.

In the following:

{ key1=value1,key2=value2 }

The delimiter is the comma ( ','). The final key-value pair does not need a delimiter.

For this transform, this parameter defines the pattern that separates the values that you want to extract into the array.

Tip: You can insert the Unicode equivalent character for this parameter value using a regular expression of the form /\uHHHH/. For example, /\u0013/ represents Unicode character 0013 (carriage return). For more information, see Supported Special Regular Expression Characters.

Usage Notes:

Required?Data Type
Yes

String (literal, regular expression, or Cloud Dataprep pattern )

quote

extractlist col: MySourceValues on:`{alpha}+` delimiter:';' quote:'\"'

Output: Extracts from the MySourceValues column each instance of a string value that occurs before the delimiter. Values between double-quotes are considered string literals and are not processed according to the delimiters defined in the transform.

Can be used to specify a string as a single quoted object. This parameter value can be one or more characters.

Usage Notes:

Required?Data Type
NoString
  • The quote value can appear anywhere in the column value. It is not limited by the constraints of any other parameters.

as

Name of the new column that is being generated. If the as parameter is not specified, a default name is used.

Usage Notes:

Required?Data Type
NoString (column name)

Examples

Example - extract query parameters from URLs

Source:

In this example, a list of URLs identifies the items in the shopping carts of visitors to your web site. You want to extract the shopping cart information embedded in the query parameters of the URL.

UsernamecartURL
joe.robinsonhttp://example123.com/cart.asp?prodid=1001&qty=2
steph.schmidthttp://example123.com/cart.asp?prodid=1005&qty=4
jack.holmeshttp://example123.com/cart.asp?prodid=2102&qty=1
tina.joneshttp://example123.com/cart.asp?prodid=10412&qty=2

The following transform extracts the list of query values from the URL. Note that the equals sign is included in the matching pattern so that you don't accidentally pick up numeric values from the non-parameter part of the URL:

extractlist col:cartURL on:`={digit}+`

The two query parameter values have been extracted into an array of values, including the equals sign, which must be removed:

replace col:cartURL with: '' on: `=` global: true

You can now unnest these values into separate columns:

unnest col:extractlist_cartURL keys:'[0]','[1]'

After you rename the two columns to prodId and Qty, you can drop the column generated by the extractlist transform.

Results:

UsernamecartURLprodIdQty
joe.robinson http://example123.com/cart.asp?prodid=1001&qty=2 10012
steph.schmidt http://example123.com/cart.asp?prodid=1005&qty=4 10054
jack.holmes http://example123.com/cart.asp?prodid=2102&qty=1 21021
tina.jones http://example123.com/cart.asp?prodid=10412&qty=2 104122

Example - Extracting counts from a ragged array using extractlist

Source:

The following dataset contains counts of support emails processed by each member of the support team for individual customers over a six-month period. In this case, you are interested in the total number of emails processed for each customer.

Unfortunately, the data is ragged, as there are no entries for a support team member if he or she has not answered an email for a customer. So, the unnest transform is not usable.

custIdstartDateendDatesupportEmailCount
C0017/15/201512/31/2015["Max":"2","Ted":"0","Sally":"12","Jack":"6","Sue":"4"]
C0027/15/201512/31/2015["Sally":"4","Sue":"3"]
C0037/15/201512/31/2015["Ted":"12","Sally":"2"]
C0047/15/201512/31/2015["Jack":"7","Sue":"4","Ted":"5"]

If the data is imported from a CSV file, you might need to make some simple replace transforms to clean up the data to look like the above example.

Transform:

Use the following transform to extract just the numeric values from the supportEmailCount array:

extractlist col:supportEmailCount on:`{digit}+`

You should now have a column extractlist_supportEmailCount containing a ragged array. You can use the following transforms to convert this data to a comma-separated list of values:

replace col: extractlist_supportEmailCount on: `[` with: '' global: true

replace col: extractlist_supportEmailCount on: `]` with: '' global: true

replace col: extractlist_supportEmailCount on: `"` with: '' global: true

Convert the column to String data type.

You can now split out the column into separate columns containing individual values in the modified source. The limit parameter specifies the number of splits to create, resulting in 5 new columns, which is the maximum number of entries in the source arrays.

split col:extractlist_supportEmailCount on:',' limit:4

You might have to set the type for each generated column to Integer. If you try to use a derive transform to calculate the sum of all of the generated columns, it only returns values for the first row because the missing rows are null values.

In the columns containing null values, select the missing value bar in the data histogram. Select the Replace suggestion card, and modify the transform to write a 0 in place of the null value, as follows:

set col: extractlist_supportEmailCount3 value: '0' row: ISMISSING([extractlist_supportEmailCount3])

Repeat this step for any other column containing null values.

You can now use the following to sum the values in the generated columns:

derive value:(extractlist_supportEmailCount1 + extractlist_supportEmailCount2 + extractlist_supportEmailCount3 + extractlist_supportEmailCount4 + extractlist_supportEmailCount5)

Results:

After renaming the generated column to totalSupportEmails and dropping the columns used to create it, your dataset should look like the following:

custIdstartDateendDatesupportEmailCounttotalSupportEmails
C0017/15/201512/31/2015["Max":"2","Ted":"0","Sally":"12","Jack":"6","Sue":"4"]24
C0027/15/201512/31/2015["Sally":"4","Sue":"3"]7
C0037/15/201512/31/2015["Ted":"12","Sally":"2"]14
C0047/15/201512/31/2015["Jack":"7","Sue":"4","Ted":"5"]16

Was this page helpful? Let us know how we did:

Send feedback about...

Google Cloud Dataprep Documentation