Drop, move, and rename columns
Queries often call for dropping, moving, or renaming columns in a table. This guide covers doing so programmatically.
Like the rest of Deephaven's table operations, these work the same on static and real-time tables. drop_columns, rename_columns, and the move methods don't copy any data: the result shares the source table's columns and updates whenever the source does.
The following table lists the methods that do each task:
| Task | Methods |
|---|---|
| Drop columns | drop_columns, select, view |
| Move columns | move_columns, move_columns_up, move_columns_down, select, view |
| Rename columns | rename_columns, move_columns, move_columns_up, move_columns_down, select, view |
The examples in this guide use the following table:
Drop columns from a table
drop_columns takes the columns to remove. view and select take the columns to keep, so they suit a table where you keep only a few. The two differ in when they compute and store data, as the sections below show. For a side-by-side comparison, see Choose the right column selection method.
drop_columns
drop_columns removes the columns you name from a table.
view
view keeps only the columns you list and removes the rest of the source table's columns. Columns given by name reuse the source table's data, while columns defined by a formula (such as Total = TestGrade + HomeworkGrade) become formula columns.
Note
A formula column stores only its formula. Deephaven calculates each value when it's accessed.
The following code block uses view to remove the StudentID column from the source table.
select
Like view, select keeps only the columns you list and removes the rest. Unlike view, select stores the data for every column in its result in memory.
Important
On large tables, select can use a lot of memory. How select handles each column depends on where its data comes from:
- A formula column:
selectcomputes the values and stores them. - A column whose data comes from files or other sources outside memory:
selectstores the data in memory. - A column whose type is a Deephaven vector:
selectalways copies it. - A column given by name whose data is already in memory:
selectreuses that data instead of copying it. The exception is a column that reads its data through a row redirection, such as a column of a sorted refreshing table.selectcopies those columns.
The following code block uses select to remove the Name column from the source table.
Move columns in a table
Choose a method based on where the columns should go:
- To place columns at a given index, use
move_columns. - To send columns to the left edge, use
move_columns_up. - To send columns to the right edge, use
move_columns_down. - To list every column in a new order, use
vieworselect. - To move and rename a column in one call, pass a rename expression such as
"ID = StudentID"to any of the move methods. See Move and rename columns together.
move_columns
move_columns moves one or more columns to a specified column index in the resulting table. Column indexes start at 0, the leftmost column.
The following code block uses move_columns twice: once to move a single column further right, and once to move multiple columns further right.
In result_move_one, StudentID lands at index 3, just before GPA. In result_move_two, Name and StudentID land at indexes 2 and 3, in the order you list them.
move_columns_up
move_columns_up moves one or more columns to the left edge of the resulting table, starting at index 0.
The following example moves StudentID to the leftmost position in the table.
move_columns_down
move_columns_down moves one or more columns to the right edge of the resulting table.
The following example moves TestGrade and HomeworkGrade to the right edge of the table, so they become the last two columns.
Reorder with view and select
view and select output columns in the order you list them. To reorder a table, list every column in the order you want. The following example puts GPA first:
Move and rename columns together
move_columns, move_columns_up, and move_columns_down also accept rename expressions, such as "ID = StudentID", so you can move and rename a column in one call. A rename expression here follows the same rules as rename_columns, including the silent replacement of an existing column with the same name. The following example uses move_columns_up to move StudentID to the leftmost position and rename it to ID:
Rename columns in a table
rename_columns renames columns in place and keeps the rest of the table's columns in their original order. view and select can also rename, but they keep only the columns you list. The move methods can rename a column as they move it; see Move and rename columns together.
rename_columns
rename_columns renames one or more columns in a table. Renaming a column follows the syntax NewColumnName = OldColumnName.
Important
If the new column name matches an existing column that the same call doesn't also rename, rename_columns silently replaces the existing column.
The following example renames StudentID to ID and GPA to GradePointAverage.
Rename with view and select
view and select rename with the same NewColumnName = OldColumnName syntax as rename_columns. As we saw when dropping columns, they keep only the columns you list, so list every column you want to keep along with the rename.
The following example uses both select and view to rename the Name column to StudentName.