Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Published by Scroll Versions from space DEV and version r0811

D toc

D s transforms

Excerpt

Counts the number of instances of a specified pattern in a column and writes that value into a newly generated column. Source column is unchanged. 

D s
snippetBasic

D code
countpattern col: myCol on: 'honda'

Output: Generates a new column containing the number of instances of the string honda that appear in each row of the column, myCol

D s
snippetParameters

D code

countpattern col:column_ref [ignoreCase:true|false] [after:start_point | from: start_point] [before:end_point | to:end_point] [on:'exact_match']

TokenRequired?Data TypeDescription
countpatternYtransformName of the transform
colYstringSource column name
ignoreCaseNbooleanIf true, matching is case-insensitive.

Matching parameters:

Info

NOTE: At least one of the following parameters must be included to specify the pattern to count: after, before, from, on, to.

TokenRequired?Data TypeDescription
afterNstringString literal or pattern that precedes the pattern to match
beforeNstringString literal or pattern that appears after the pattern to match
fromNstringString literal or pattern that identifies the start of the pattern to match
onNstringString literal or pattern that identifies the pattern to match.
toNstringString literal or pattern that identifies the end of the pattern to match

D s lang notes

col

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

D code

countpattern col: MyCol on: 'MyString'

Output: Counts the number of instances of the value MyString in the MyCol column and writes this value to a new column. 

D s
snippetusage

Required?Data Type
YesString (column name)

after

D code

countpattern col: MyCol after: 'entry:'

Output: Counts 1 if there is anything that appears in the MyCol column value after the string entry:If the value entry: does not appear in the column, the output value is 0.

Include Page
after Parameter
after Parameter

D s
snippetusage

Required?Data Type
NoString (string literal or pattern)
  • The after and from parameters are very similar. from includes the matching value as part of the extracted string.
  • after can be used with either toon, or before. See Pattern Clause Position Matching

before

Include Page
before Parameter
before Parameter

D code

countpattern col: MyCol before: '|'

Output:

  • Counts 1 if there is a value that appears before the pipe character  (|) in the MyCol column, and no other pattern parameter is specified. If the before value does not appear in the column, the output value is 0.
  • If another pattern parameter such as after is specified, the total count of instances is written to the new column.

D s
snippetusage

Required?Data Type
NoString or pattern
  • The before and to parameters are very similar.  to includes the matching value as part of the extracted string.
  • before can be used with either from on, or afterSee  Pattern Clause Position Matching .

from

Include Page
from Parameter
from Parameter

D code

countpattern col: MyCol from: 'go:'

Output: 

  • Counts 1 if contents from MyCol that occur from go:, to the end of the cell when no other pattern parameter is specified. If go: does not appear in the column, the output value is blank.
  • If another pattern parameter such as to is specified, the total count of instances is written to the new column.

D s
snippetusage

Required?Data Type
NoString or pattern
  • The after and from parameters are very similar.  from includes the matching value as part of the extracted string.
  • from can be used with either to or beforeSee  Pattern Clause Position Matching .

on

Include Page
on Parameter
on Parameter

D code

countpattern col: MyCol on: `###ERROR`

Tip

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.

 

D s
snippetusage

Required?Data Type
No

String (literal, regular expression, or

D s lang
itempattern
rtrue

to

Include Page
to Parameter
to Parameter

D code

countpattern col:MyCol from:'note:' to: `/`

Output:

  • Counts instances from MyCol column of all values that begin with note: up to a backslash character.
  • If a second pattern parameter is not specified, then this value is either 0 or 1.

D s
snippetusage

Required?Data Type
NoString or pattern
  • The before and to parameters are very similar.  to includes the matching value as part of the extracted string.
  • to can be used with either from or after. See Pattern Clause Position Matching.

ignoreCase

Include Page
ignoreCase Parameter
ignoreCase Parameter

D code

countpattern col: MyCol on: 'My String' ignoreCase: true

Output: Counts the instances of the following values if they appear in the MyCol column: My Stringmy stringMy string, etc.

D s
snippetusage

Required?Data Type
NoBoolean


D s
snippetExamples

Example - counting patterns in tweets

Include Page
EXAMPLE - Countpattern Transform
EXAMPLE - Countpattern Transform

D s also
labelwrangle_transform_countpattern