Change Order of Pandas DataFrame Columns – All the Ways possible

When working with Pandas DataFrames, the column order can sometimes be important. Whether you’re cleaning data, preparing it for visualization, or just making it more readable, changing column order is a common task.

In this post, we’ll explore multiple ways to rearrange columns in a Pandas DataFrame. Ready? Let’s dive in! 💡


1. Rearranging Columns Using a Custom List 🎯

One of the simplest ways to reorder columns is by specifying the column names in a list.

import pandas as pd

# Sample DataFrame
data = {
    'Name': ['Alice', 'Bob', 'Charlie'],
    'Age': [25, 30, 35],
    'City': ['New York', 'Los Angeles', 'Chicago'],
    'Salary': [70000, 80000, 90000]
}
df = pd.DataFrame(data)

# Reorder Columns
new_order = ['Name', 'City', 'Age', 'Salary']
df = df[new_order]
print(df)

✅ This method is great when you already know the exact order you want.


2. Moving a Specific Column to the Front 🚀

Want to bring a particular column to the front? Use this trick!

# Move 'Salary' to the first position
df = df[['Salary'] + [col for col in df.columns if col != 'Salary']]
print(df)

✅ Useful when you want to highlight a specific column without manually listing all the others.


3. Sorting Columns Alphabetically 🔤

Need your columns in alphabetical order? This method sorts them automatically!

df = df[sorted(df.columns)]
print(df)

✅ Perfect for data consistency and organization.

Advertisements

4. Rearranging Using Index Positions 📊

Sometimes, you might want to reorder columns by their index positions rather than names.

# Move the last column to the first position
cols = df.columns.tolist()
df = df[[cols[-1]] + cols[:-1]]
print(df)

✅ This is helpful when working with datasets where column names might change.


5. Using df.insert() to Move Columns 🎯

The .insert() function allows us to insert a column at a specific position.

# Move 'Salary' to the second position
df.insert(1, 'Salary', df.pop('Salary'))
print(df)

✅ Use this when you need to place a column at a specific index dynamically.


6. Reordering Columns Dynamically

If you don’t know the exact column names but want to reorder based on conditions, you can use filters.

# Move all numerical columns to the front
num_cols = df.select_dtypes(include=['number']).columns.tolist()
other_cols = [col for col in df.columns if col not in num_cols]
df = df[num_cols + other_cols]
print(df)

✅ Ideal for handling large datasets with mixed column types.


Final Thoughts 🎉

Reordering columns in Pandas isn’t just about aesthetics—it helps with data analysis, readability, and structuring reports. Whether you’re manually setting the order or using dynamic approaches, these techniques will make your workflow smoother and more efficient.

🚀 Which method do you find most useful? Let me know in the comments! 📝

Advertisements

Leave a comment

Website Powered by WordPress.com.

Up ↑

Discover more from BrontoWise

Subscribe now to keep reading and get access to the full archive.

Continue reading