Skip to main content
Version: Java (Groovy)

Drop, move, and rename columns

Queries often call for dropping, moving, or renaming columns in a table. This guide covers doing so programmatically.

The following methods drop columns from a table:

The following methods move columns in a table:

The following methods rename columns in a table:

The examples in this guide will use the following table:


source = newTable(
stringCol("Name", "Andy", "Claire", "Jane", "Steven"),
intCol("StudentID", 1, 2, 3, 4),
intCol("TestGrade", 85, 95, 88, 72),
intCol("HomeworkGrade", 85, 95, 90, 95),
doubleCol("GPA", 3.0, 4.0, 3.7, 2.8),
)

Remove columns from a table

dropColumns

dropColumns removes one or more columns from a table based on the column names it's given.

resultDropOneCol = source.dropColumns("GPA")
resultDropTwoCols = source.dropColumns("TestGrade", "HomeworkGrade")

view

view removes any columns not given as input that are present in the source table. If view is not given any input, it returns a table the same as the source table but with all formula columns.

info

Formula columns initially store only the formulas used to create columnar data. Values are calculated on the fly as needed.

The following code block uses view to remove the StudentID column from the source table.

resultNoStudentId = source.view("Name", "TestGrade", "HomeworkGrade", "GPA")

select

select is similar to view in that it removes any columns not given as input that are present in the source table. However, unlike view,select creates in-memory columns.

info

In-memory columns store all of their data in memory. This includes columns of any type, such as formula or memoized columns. Be careful when using select with large tables, as it can consume a lot of memory.

The following code block uses select to remove the Name column from the source table.

resultNoName = source.select("StudentID", "TestGrade", "HomeworkGrade", "GPA")

Move columns in a table

moveColumns

moveColumns moves one or more columns to a specified column index in the resulting table.

The following code block uses moveColumns twice: once to move a single column further right, and once to move multiple columns further right.

resultMoveOne = source.moveColumns(3, "StudentID")
resultMoveTwo = source.moveColumns(2, "Name", "StudentID")

moveColumnsUp

moveColumnsUp moves one or more columns to the zeroth column index (the left) in the resulting table.

The following example moves StudentID to the leftmost position in the table.

resultStudentIdFirst = source.moveColumnsUp("StudentID")

moveColumnsDown

moveColumnsDown moves one or more columns to the last column index (the right) in the resulting table.

The following example moves TestGrade and HomeworkGrade to the rightmost position in the table.

resultGradesLast = source.moveColumnsDown("TestGrade", "HomeworkGrade")

Rename columns in a table

renameColumns

renameColumns renames one or more columns in a table. Renaming a column follows the syntax NewColumnName = OldColumnName.

The following example renames StudentID to ID and GPA to GradePointAverage.

resultRenamed = source.renameColumns("ID = StudentID", "GradePointAverage = GPA")

view and select

Renaming columns with view and select follows the same syntax as renameColumns (NewColumnName = OldColumnName). As we saw previously when removing columns, select and view take a list of column names as input.

The following example uses both select and view to rename the Name column to StudentName.

info

select creates in-memory columns. Be careful when using select with large tables, as it can consume a lot of memory.

resultSelectRenamed = source.select(
"StudentName = Name", "StudentID", "TestGrade", "HomeworkGrade", "GPA"
)
resultViewRenamed = source.view(
"StudentName = Name", "StudentID", "TestGrade", "HomeworkGrade", "GPA"
)