Move columns to the front in Pandas DataFrame
Python has a Pandas
data analysis library called Pandas. We can use Pandas in Python to perform many different types of operations on DataFrames.
This guide will explain the method to reorder the columns in a pandas DataFrame to move them to the front.
Move a column to the front in a Pandas DataFrame
Suppose we want to rearrange the positions of the columns in a DataFrame. Pandas provides insert()
and reindex()
methods that move a single column to the front of a Pandas DataFrame.
Use pop()
and insert()
Method
The idea is to remove the column we want to move to the front using the remove() method. Then insert that column again into the DataFrame using the remove() method pop()
by specifying the position at which we want to insert it .insert()
In the following code, we have a DataFrame consisting of student records. Our goal is to move 名称
the columns to the front.
First, we will pop()
remove it using the method, pop()
which takes as a parameter the label of the column we want to remove and returns it. We store the returned column in col
.
Using the method, we push the columns back to the DataFrame insert()
by defining the column position as (first column position), column name, and actual column).loc=0
Sample code:
# Python 3.x
import pandas as pd
student = {
"Course": ["Java", "Python", "C++", "Dart"],
"Marks": [70, 80, 90, 60],
"Age": [19, 20, 21, 19],
"Name": ["Jhon", "Aliya", "Nate", "Amber"],
}
df = pd.DataFrame(student)
print(df)
col = df.pop("Name")
df.insert(loc=0, column="Name", value=col)
print(df)
Output:
$python3 Main.py
Course Marks Age Name
0 Java 70 19 Jhon
1 Python 80 20 Aliya
2 C++ 90 21 Nate
3 Dart 60 19 Amber
Name Course Marks Age
0 Jhon Java 70 19
1 Aliya Python 80 20
2 Nate C++ 90 21
3 Amber Dart 60 19
Usagereindex()
We can 重新索引
rearrange the columns by using them. reindex()
method to reorder the columns.
This method pandas.DataFrame
is available in the dataframe module. We will provide the list of column names in the order we want and pass the list
and DataFrame
to the dataframe reindex()
method to return the updated DataFrame.
Sample code:
# Python 3.x
import pandas as pd
student = {
"Course": ["Java", "Python", "C++", "Dart"],
"Marks": [70, 80, 90, 60],
"Age": [19, 20, 21, 19],
"Name": ["Jhon", "Aliya", "Nate", "Amber"],
}
df = pd.DataFrame(student)
print(df)
df = pd.DataFrame.reindex(df, columns=["Name", "Course", "Marks", "Age"])
print(df)
Output:
$python3 Main.py
Course Marks Age Name
0 Java 70 19 Jhon
1 Python 80 20 Aliya
2 C++ 90 21 Nate
3 Dart 60 19 Amber
Name Course Marks Age
0 Jhon Java 70 19
1 Aliya Python 80 20
2 Nate C++ 90 21
3 Amber Dart 60 19
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Convert Tensor to NumPy array in Python
Publish Date:2025/05/03 Views:85 Category:Python
-
This tutorial will show you how to convert a Tensor to a NumPy array in Python. Use the function in Python Tensor.numpy() to convert a tensor to a NumPy array Eager Execution of TensorFlow library can be used to convert tensor to NumPy arra
Saving NumPy arrays as images in Python
Publish Date:2025/05/03 Views:193 Category:Python
-
In Python, numpy module is used to manipulate arrays. There are many modules available in Python that allow us to read and store images. An image can be thought of as an array of different pixels stored at specific locations with correspond
Transposing a 1D array in NumPy
Publish Date:2025/05/03 Views:98 Category:Python
-
Arrays and matrices form the core of this Python library. The transpose of these arrays and matrices plays a vital role in certain topics such as machine learning. In NumPy, it is easy to calculate the transpose of an array or a matrix. Tra
Find the first index of an element in a NumPy array
Publish Date:2025/05/03 Views:58 Category:Python
-
In this tutorial, we will discuss how to find the first index of an element in a numpy array. Use where() the function to find the first index of an element in a NumPy array The function in the numpy module where() is used to return an arra
Remove Nan values from NumPy array
Publish Date:2025/05/03 Views:118 Category:Python
-
This article discusses some built-in NumPy functions that you can use to remove nan values. Remove Nan values using logical_not() and methods in NumPy isnan() logical_not() is used to apply logical NOT to the elements of an array. isn
Normalizing a vector in Python
Publish Date:2025/05/03 Views:51 Category:Python
-
A common concept in the field of machine learning is to normalize a vector or dataset before passing it to the algorithm. When we talk about normalizing a vector, we say that its vector magnitude is 1, being a unit vector. In this tutorial,
Calculating Euclidean distance in Python
Publish Date:2025/05/03 Views:128 Category:Python
-
In the world of mathematics, the shortest distance between two points in any dimension is called the Euclidean distance. It is the square root of the sum of the squares of the differences between the two points. In Python, the numpy, scipy
Element-wise division in Python NumPy
Publish Date:2025/05/03 Views:199 Category:Python
-
This tutorial shows you how to perform element-wise division on NumPy arrays in Python. NumPy Element-Wise Division using numpy.divide() the function If we have two arrays and want to divide each element of the first array with each element
Convert 3D array to 2D array in Python
Publish Date:2025/05/03 Views:79 Category:Python
-
In this tutorial, we will discuss the methods to convert 3D array to 2D array in Python. numpy.reshape() Convert 3D array to 2D array using function in Python [ numpy.reshape() Function](numpy.reshape - NumPy v1.20 manual)Changes the shape