Pandas DataFrame: How to change the order of columns (5 examples)

Updated: February 22, 2024 By: Guest Contributor Post a comment

Introduction

Pandas is a vital tool in the data scientist’s toolbox, widely used for data manipulation and analysis in Python. One common task when working with Pandas DataFrames is rearranging the order of columns. Whether for better organization, to prepare data for plotting, or to meet the requirements of a specific analysis method, changing the column order can be crucial. In this tutorial, we will explore five ways to change the order of columns in a Pandas DataFrame, progressing from basic to more advanced examples.

Preparing a Sample DataFrame to Use

Before diving into the examples, ensure you have Python and Pandas installed in your environment. You can install Pandas using pip:

pip install pandas

Let’s start by creating a sample DataFrame to work with throughout this tutorial:

import pandas as pd
df = pd.DataFrame({
    'Name': ['Alice', 'Bob', 'Charlie', 'David'],
    'Age': [25, 30, 35, 40],
    'City': ['New York', 'Los Angeles', 'Chicago', 'Miami'],
    'Salary': [50000, 60000, 70000, 80000]
})
print(df)

Example 1: Rearrange Columns by Name

The simplest way to change the order of DataFrame columns is by listing them in the desired order:

df = df[['City', 'Name', 'Age', 'Salary']]
print(df)

This will reorder the DataFrame columns as ‘City’, ‘Name’, ‘Age’, ‘Salary’. It’s straightforward and effective for small DataFrames.

Example 2: Using reindex Method

To change the columns order more dynamically, you can use the reindex method, specifying the columns parameter with the desired order:

df = df.reindex(columns=['Salary', 'City', 'Name', 'Age'])
print(df)

This method is particularly useful when dealing with a large number of columns or when the new order is not hardcoded.

Example 3: Organizing Columns by Data Type

Sometimes, you may want to group columns by their data type. Here’s how you can achieve this:

dtype_groups = df.columns.to_series().groupby(df.dtypes).groups
sorted_columns = [col for dtype, col_list in dtype_groups.items() for col in sorted(col_list)]
df = df[sorted_columns]
print(df)

This approach organizes columns alphabetically within their data type groups.

Example 4: Moving a Column to First or Last Position

If you specifically want to move one column to the beginning or the end, you can do so as follows:

To move a column to the start:

col_name = 'Age'
first_column = df.pop(col_name)
df.insert(0, col_name, first_column)
print(df)

To move a column to the end:

df[col_name] = df.pop(col_name)
print(df)

These methods are convenient for emphasizing or de-emphasizing certain columns.

Example 5: Advanced Rearrangement with Custom Functions

For complex rearrangements, such as based on conditions or external inputs, you can combine Python’s flexibility with Pandas to create custom column orders. Here’s an example where we sort columns based on their mean values:

column_means = df.mean()
sorted_columns = column_means.sort_values(ascending=False).index.tolist()
df = df[sorted_columns]
print(df)

This method sorts the columns from highest to lowest based on their mean value, showcasing the power of combining Python logic with Pandas for data manipulation.

Conclusion

Mastering the rearrangement of DataFrame columns in Pandas can significantly streamline your data preprocessing and analysis workflows. By progressing through these examples, from basic reordering by name to advanced manipulations based on data characteristics, you’ll be well-equipped to handle various data restructuring needs. Remember, the key to fluid data manipulation is understanding both the tools at your disposal and the specific requirements of your analysis.