Skip to main content

DB_Table.add_group_number

add_group_numbergrouping_methodnamefromstepon_problems

Group: Values
Aliases: add group column, group id, bucket, tile

Documentation

Adds a new column to the table enumerating groups of rows, assigning each row to one group number. All rows in each group will get the same number.

Arguments

  • grouping_method: Specifies how to group the rows; see "Grouping Methods", below.
  • name: The name of the new column. Defaults to "Group".
  • from: The starting value for the enumeration. Defaults to 0.
  • step: The amount to increment the enumeration by. Defaults to 1.

Examples

Assign group numbers based on unique values of the first two columns.

     ## table:
x | y | z
---+---+---
1 | 0 | 2
0 | 1 | 0
1 | 2 | 0
0 | 1 | 1
1 | 0 | 1
1 | 2 | 1
table = table_builder [['x', [1, 0, 1, 0, 1, 1]], ['y', [0, 1, 2, 1, 0, 2]], ['z' [2, 0, 0, 1, 1, 1]]]
table2 = table.add_group_number (..Unique group_by=['x', 'y']) "g"
table2.at 'g' . to_vector
# => [0, 1, 2, 1, 0, 2]
## table2:
x | y | z | g
---+---+---+---
1 | 0 | 2 | 0
0 | 1 | 0 | 1
1 | 2 | 0 | 2
0 | 1 | 1 | 1
1 | 0 | 1 | 2
1 | 2 | 1 | 0

Divide rows into three groups.

     ## table:
x | y
---+---
1 | 5
2 | 4
3 | 3
4 | 2
5 | 1
table = table_builder [['x', [1, 2, 3, 4, 5]], ['y', [5, 4, 3, 2, 1]]]
table2 = tabble.add_group_number (..Equal_Count 3) "g"
table2.at 'g' . to_vector
# => [0, 0, 1, 1, 2]
## table2:
x | y | g
---+---+---
1 | 5 | 0
2 | 4 | 0
3 | 3 | 1
4 | 2 | 1
5 | 1 | 2

Errors

  • If the columns specified in group_by or order_by are not present in the table, a Missing_Input_Columns error is raised.
  • If the column with the same name as provided name already exists, a Duplicate_Output_Column_Names problem is reported and the existing column is renamed to avoid the clash.
  • If grouping on floating point numbers, a Floating_Point_Equality problem is reported.

Remarks

Grouping Methods

The following grouping methods are supported:

  • Unique: Group rows by the specified columns.
  • Equal_Count: Create the specified number of groups with the same number of rows in each group (except possibly the last one).

Ordering of rows

Note that the ordering of rows from the original table is preserved in all cases. The grouping and ordering settings can affect how the group numbers are assigned, depending on the grouping method. The order of the rows itself is not changed by this operation.