Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python - Move a column to the first position in Pandas DataFrame?
Use pop() to remove a column and insert() to place it at the first position in a Pandas DataFrame. This technique allows you to reorder columns efficiently without creating a new DataFrame.
Creating the DataFrame
First, let's create a sample DataFrame with three columns ?
import pandas as pd
# Create DataFrame
dataFrame = pd.DataFrame(
{
"Student": ['Jack', 'Robin', 'Ted', 'Marc', 'Scarlett', 'Kat', 'John'],
"Result": ['Pass', 'Fail', 'Pass', 'Fail', 'Pass', 'Pass', 'Pass'],
"Roll Number": [5, 10, 3, 8, 2, 9, 6]
}
)
print("Original DataFrame:")
print(dataFrame)
Original DataFrame:
Student Result Roll Number
0 Jack Pass 5
1 Robin Fail 10
2 Ted Pass 3
3 Marc Fail 8
4 Scarlett Pass 2
5 Kat Pass 9
6 John Pass 6
Moving Column to First Position
To move the "Roll Number" column to the first position, we use pop() to extract the column and insert() to place it at index 0 ?
import pandas as pd
# Create DataFrame
dataFrame = pd.DataFrame(
{
"Student": ['Jack', 'Robin', 'Ted', 'Marc', 'Scarlett', 'Kat', 'John'],
"Result": ['Pass', 'Fail', 'Pass', 'Fail', 'Pass', 'Pass', 'Pass'],
"Roll Number": [5, 10, 3, 8, 2, 9, 6]
}
)
print("Original DataFrame:")
print(dataFrame)
# Pop the column to extract it
shiftPos = dataFrame.pop("Roll Number")
# Insert column at the first position (index 0)
dataFrame.insert(0, "Roll Number", shiftPos)
print("\nDataFrame after moving 'Roll Number' to first position:")
print(dataFrame)
Original DataFrame:
Student Result Roll Number
0 Jack Pass 5
1 Robin Fail 10
2 Ted Pass 3
3 Marc Fail 8
4 Scarlett Pass 2
5 Kat Pass 9
6 John Pass 6
DataFrame after moving 'Roll Number' to first position:
Roll Number Student Result
0 5 Jack Pass
1 10 Robin Fail
2 3 Ted Pass
3 8 Marc Fail
4 2 Scarlett Pass
5 9 Kat Pass
6 6 John Pass
How It Works
The process involves two steps:
- pop("column_name"): Removes and returns the specified column as a Series
- insert(index, "column_name", data): Inserts the column at the specified position
The insert() method takes three parameters: the position index (0 for first), the column name, and the column data.
Alternative Method Using Column Reordering
You can also reorder columns by creating a new column list ?
import pandas as pd
# Create DataFrame
dataFrame = pd.DataFrame(
{
"Student": ['Jack', 'Robin', 'Ted'],
"Result": ['Pass', 'Fail', 'Pass'],
"Roll Number": [5, 10, 3]
}
)
# Reorder columns by specifying the desired order
cols = dataFrame.columns.tolist()
cols = [cols[2]] + cols[:2] # Move last column to front
dataFrame = dataFrame[cols]
print("Reordered DataFrame:")
print(dataFrame)
Reordered DataFrame: Roll Number Student Result 0 5 Jack Pass 1 10 Robin Fail 2 3 Ted Pass
Conclusion
Use pop() and insert() for moving columns to specific positions in Pandas DataFrames. This method modifies the DataFrame in-place and is efficient for column reordering operations.
