Rearrange or order columns in the Pandas DataFrame
In pandas, we can rearrange the columns of a DataFrame by specifying a new order for the columns.
Example: Let’s create a dataframe first.
Python
# Import the package import pandas as pd # Create a DataFrame mydata=pd.read_csv("employees.csv") mydata
The output of the above code is shown below:
Let’s rearrange the columns in the dataframe.
Python
# Specify the new order of columns new_order = ["Name", "Country", "Salary", "Gender"] final=mydata[new_order] final
The output of the above code is shown below: