Reorder pandas DataFrame Columns


This entry is part 5 of 9 in the series pandas DataFrame

To reorder columns in a Pandas DataFrame, pass into the DataFrame a list of column names in the new order. This is not sorting the rows. This will rearrange the columns into your specified order. This is not sorting rows. If you only want to show some of the columns, you can simply include the ones you want to include in the DataFrame. Doing this will only print the DataFrame with the columns rearranged in your chosen order. If we compare SQL language, this is analogous to providing a list of column names right after the SELECT keyword.

Let’s run through some Python and pandas code to show this by example. Let’s create a DataFrame manually.

import pandas as pd    # import the pandas library into Python
data = {'firstname': ['Bob', 'Sally', 'Suzie', 'Rohan'],
       'amount': [12, 67, 33, 41],
       'group': ['B', 'A', 'A', 'B']}
df = pd.DataFrame(data)
df

If you only want to show some of the columns, you can simply include the ones you want to include in the DataFrame. Doing this will only print the DataFrame with the columns rearranged in your chosen order.

df[['group', 'amount']]

If you want to modify the original DataFrame, you’ll need to reassign the reordered dataframe back to the original dataframe variable.

df = df[['group', 'firstname', 'amount']]

Compare SQL Language

By way of comparison, in SQL language, how would you permanently remove a column from a table? The SQL syntax for that is ALTER TABLE table_name DROP COLUMN column_name;

Here below are some inputs and outputs, related to the columns.

df.columns
Index(['group', 'firstname', 'amount'], dtype='object')

type(df.columns)
pandas.core.indexes.base.Index

lst_col = list(df.columns)
lst_col
['group', 'firstname', 'amount']

type(lst_col)
list

Series Navigation<< Display All of the Columns in a DataFrameSorting a pandas DataFrame >>

Leave a comment

Your email address will not be published. Required fields are marked *