Open In App

How to Move a Column to First Position in Pandas DataFrame?

Last Updated : 19 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Moving a column to the first position in a Pandas DataFrame means changing the column order so that the column you want appears first. For example, if you have a DataFrame with columns ['Age', 'Name', 'City'] and you want to move the 'Name' column to the front, the result will be ['Name', 'Age', 'City']. This can be done in several easy and efficient ways using Pandas. Let’s explore some common methods.

Using pop()

Remove the column with pop() and insert it at the front with insert(). This modifies the DataFrame efficiently in-place and is often the fastest method.

Python
import pandas as pd
d = {'Age': [25, 30, 22, 28], 
     'Name': ['Alice', 'Bob', 'Charlie', 'David'], 
     'City': ['NY', 'LA', 'Chicago', 'Houston']}
df = pd.DataFrame(d)

col = df.pop('Name') 
df.insert(0, 'Name', col)  
print(df)

Output
      Name  Age     City
0    Alice   25       NY
1      Bob   30       LA
2  Charlie   22  Chicago
3    David   28  Houston

Explanation:

  • df.pop('Name') removes the 'Name' column from df and saves it in col.
  • df.insert(0, 'Name', col) inserts the 'Name' column back at the first position (index 0) in df.

Using list concatenation

Create a new column order by putting the target column first, then all others. Reorder the DataFrame by selecting columns in this new order. It’s simple and easy to understand.

Python
import pandas as pd
d = {'Age': [25, 30, 22, 28], 
     'Name': ['Alice', 'Bob', 'Charlie', 'David'], 
     'City': ['NY', 'LA', 'Chicago', 'Houston']}

df = pd.DataFrame(d)
df = df[['Name'] + [col for col in df.columns if col != 'Name']]
print(df)

Output
      Name  Age     City
0    Alice   25       NY
1      Bob   30       LA
2  Charlie   22  Chicago
3    David   28  Houston

Explanation: List concatenation creates a new column list starting with 'Name', followed by all other columns except 'Name'. The DataFrame’s columns are then reordered based on this list, placing 'Name' first and keeping the rest in their original order.

Using .loc

Convert columns to a list, move the target column to the front, then use .loc to reorder the DataFrame. Flexible and explicit, good for more complex reordering.

Python
import pandas as pd
d = {'Age': [25, 30, 22, 28], 
     'Name': ['Alice', 'Bob', 'Charlie', 'David'], 
     'City': ['NY', 'LA', 'Chicago', 'Houston']}

df = pd.DataFrame(d)
cols = df.columns.tolist()
cols.insert(0, cols.pop(cols.index('Name')))
df = df.loc[:, cols]
print(df)

Output
      Name  Age     City
0    Alice   25       NY
1      Bob   30       LA
2  Charlie   22  Chicago
3    David   28  Houston

Explanation:

  • df.columns.tolist() converts the DataFrame columns to a list.
  • cols.pop(cols.index('Name')) finds and removes 'Name' from its current position in the list.
  • cols.insert(0, ...) inserts 'Name' at the beginning of the columns list.
  • df.loc[:, cols] reindexes the DataFrame to use the new column order.

Using re.index()

Specify the exact column order in a list and pass it to reindex(). This method clearly sets the DataFrame’s columns to your desired arrangement.

Python
import pandas as pd

d = {'Age': [25, 30, 22, 28], 
     'Name': ['Alice', 'Bob', 'Charlie', 'David'], 
     'City': ['NY', 'LA', 'Chicago', 'Houston']}

df = pd.DataFrame(d)

cols = ['Name'] + [col for col in df.columns if col != 'Name']
df = df.reindex(columns=cols)
print(df)

Output
      Name  Age     City
0    Alice   25       NY
1      Bob   30       LA
2  Charlie   22  Chicago
3    David   28  Houston

Explanation:

  • ['Name'] + [col for col in df.columns if col != 'Name'] makes a list starting with 'Name', followed by all other columns except 'Name'.
  • df.reindex(columns=cols) rearranges the DataFrame columns according to the new list.

Next Article

Similar Reads