ColumnRearrange
Modifies the order of columns within a dataframe by either placing column at the front or back of a dataframe, or at a specified index. You can specify one or more columns to move. If no index is specified, the columns are placed at the front of the dataframe. Otherwise, the index can be specified using either the front, back, or indexes options.
Options
columns: Specifies the columns to move within the dataframe
indexes: Specifies the indexes in which to place the specified columns
front: Flag specifying to move the specified columns to the front of the dataframe
back: Flag specifying to move the specified columns to the back of the dataframe
Examples
Example 1 - Move a Single Column to the Front
When a single column is specified, and no other options are used, that column will be moved to the very front of the dataframe. All other columns will retain their order behind it. This is helpful when highlighting a specific column of interest for analysis or visualization.
#> ColumnRearrange DeviceID
AFLEFT
columnsToMove = ['DeviceID']
columnsToMove = columnsToMove + [column for column in bankTransactionsDf.columns if column not in columnsToMove]
bankTransactionsDf = bankTransactionsDf[columnsToMove] AFRIGHT
Example 2 - Move Multiple Columns to the Front
When multiple columns are listed without specifying target indexes, they will be moved to the front of the dataframe in the order provided. This is useful when you want key columns to appear first without altering the remaining column order.
#> ColumnRearrange TransactionDate MerchantID Channel
AFLEFT
columnsToMove = ['TransactionDate', 'MerchantID', 'Channel']
columnsToMove = columnsToMove + [column for column in bankTransactionsDf.columns if column not in columnsToMove]
bankTransactionsDf = bankTransactionsDf[columnsToMove] AFRIGHT
Example 3 - Move Column by Index Position
Instead of specifying a column name, you can specify a column by its index position. This will move the column at the given index, in this case column at index 3, to the front of the dataframe.
#> ColumnRearrange 3
AFLEFT
columnsToMove = [bankTransactionsDf.columns[3]]
columnsToMove = columnsToMove + [column for column in bankTransactionsDf.columns if column not in columnsToMove]
bankTransactionsDf = bankTransactionsDf[columnsToMove] AFRIGHT
Example 4 - Move a Mix of Column Names and Indexes
A mix of column names and index references can be passed in a single call. Each listed column will be moved to the front in the order provided, regardless of whether it was specified by name or by index.
#> ColumnRearrange CustomerAge 4 5 CustomerOccupation
AFLEFT
columnsToMove = ['CustomerAge', bankTransactionsDf.columns[4], bankTransactionsDf.columns[5], 'CustomerOccupation']
columnsToMove = columnsToMove + [column for column in bankTransactionsDf.columns if column not in columnsToMove]
bankTransactionsDf = bankTransactionsDf[columnsToMove] AFRIGHT
Example 5 - Move Column to a Specified Index
By pairing each column name with a specific index, you can move that column to an exact location within the dataframe. In this case, the IP Address column is moved to the 3rd position (zero-based index), while preserving the order of all other columns.
#> ColumnRearrange --columns IP Address --indexes 3
AFLEFT
columnsToMove = ['IP Address']
[3] = [3]
remaining_columns = [column for column in bankTransactionsDf.columns if column not in columnsToMove]
for column,targetIndex in zip(columnsToMove, [3]):
remaining_columns.insert(targetIndex, column)
bankTransactionsDf = bankTransactionsDf[remaining_columns] AFRIGHT
Example 6 - Move Multiple Columns to Specific Indexes
When you specify multiple columns and corresponding target indexes, each column is moved to the index specified. This allows fine-grained reordering where each column lands exactly where you want it.
#> ColumnRearrange --columns AccountBalance LoginAttempts TransactionType --indexes 2 4 6
AFLEFT
columnsToMove = ['AccountBalance', 'LoginAttempts', 'TransactionType']
[2, 4, 6] = [2, 4, 6]
remaining_columns = [column for column in bankTransactionsDf.columns if column not in columnsToMove]
for column,targetIndex in zip(columnsToMove, [2, 4, 6]):
remaining_columns.insert(targetIndex, column)
bankTransactionsDf = bankTransactionsDf[remaining_columns] AFRIGHT
Example 7 - Move Columns to the End of the Dataframe
When the back option is used, the specified columns (by name or index) will be moved to the end of the dataframe, while preserving the order of all other columns. This is useful for deprioritizing less important fields without dropping them.
#> ColumnRearrange TransactionID AccountID 4 --back
AFLEFT
columnsToMove = ['TransactionID', 'AccountID', bankTransactionsDf.columns[4]]
columnsToMove = [column for column in bankTransactionsDf.columns if column not in columnsToMove] + columnsToMove
bankTransactionsDf = bankTransactionsDf[columnsToMove] AFRIGHT