Skip to main content
Version: Python

How to drop, move, and rename columns

This guide shows you how to perform some basic adjustments to the columns in your tables.

We'll cover the following methods:

The examples in this guide use a table called students that contains data on four students in a class. If you're unfamiliar with the new_table method, check out our guide Create a new table.

from deephaven import new_table
from deephaven.column import int_col, double_col, string_col

students = new_table(
[
string_col("Name", ["Andy", "Claire", "Jane", "Steven"]),
int_col("StudentID", [1, 2, 3, 4]),
int_col("TestGrade", [85, 95, 88, 72]),
int_col("HomeworkGrade", [85, 95, 90, 95]),
double_col("GPA", [3.0, 4.0, 3.7, 2.8]),
]
)

Remove columns from a table

drop_columns removes columns from a table when you supply the names of the columns you want to remove. select and view also remove columns; however, for these methods, you supply the names of the columns you'd like to keep. Unlike select and view, however, drop_columns won't change column types. See Choose the right selection method for more details.

drop_columns

The drop_columns method creates a table with the same number of rows as the source table, but omits any columns included in its argument. This method is useful when you only want to eliminate a small number of columns from the source table, or in cases where you need to add a column for some operation, but then no longer need it after the operation is complete.

In the following example, we drop the StudentID column by providing its name as an argument to the drop_columns method.

students_no_ID = students.drop_columns(cols=["StudentID"])

Move columns within the table

move_columns

The move_columns method moves a specified column (or a set of columns) to a different location in the resulting table determined by a specific column index value.

In the following example, we move the "GPA" to column index 1. Deephaven uses a zero-based index model, so this moves the column to the second position in the table.

move_GPA = students.move_columns(idx=1, cols=["GPA"])

Column sets can also be moved. In the following example, we move the "TestGrade" and "HomeworkGrade" columns to position 3, so they become the last two columns in the table. The first column specified in the move_columns argument takes the index position, and additional columns will be placed to its right, as shown below.

move_grades = students.move_columns(idx=3, cols=["TestGrade", "HomeworkGrade"])

move_columns_up

The move_columns_up method moves a column (or a set of columns) to the zero column index position in the resulting table. Unlike move_columns, the argument for move_columns_up does not require the column index number.

In the following example, "StudentID" becomes the first column in the table.

move_up_student_ID = students.move_columns_up(cols=["StudentID"])

move_columns_down

The move_columns_down method moves a column (or a set of columns) to the end of the resulting table.

In the following example, StudentID becomes the last column in the table.

move_down_student_id = students.move_columns_down(cols=["StudentID"])

Rename columns in a table

Deephaven provides several options to rename columns in your table. In more complex queries, you might do this when performing joins or other calculations on your data. However, the rename_columns method will rename specified columns without altering the data in any way.

rename_columns

The rename_columns method renames a specified column or columns. The order within the argument is newColumnName = oldColumnName.

In the following example, we rename the StudentID column to simply ID.

rename_student_id = students.rename_columns(cols=["ID = StudentID"])