- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to change the order of Pandas DataFrame columns?
To change the order of DataFrame columns, we can take the following Steps −
Steps
Make two-dimensional, size-mutable, potentially heterogeneous tabular data, df.
Print the input DataFrame.
Get the list of DataFrame columns, using df.columns.tolist()
Change the order of DataFrame columns.
Modify the order of columns of the DataFrame.
Print the DataFrame after changing the columns order.
Example
import pandas as pd df = pd.DataFrame( { "x": [5, 2, 1, 9], "y": [4, 1, 5, 10], "z": [4, 1, 5, 0] } ) print "Input DataFrame is:
", df cols = df.columns.tolist() cols = cols[-1:] + cols[:-1] df = df[cols] print "After changing the order:
", df
Output
Input DataFrame is: x y z 0 5 4 4 1 2 1 1 2 1 5 5 3 9 10 0 After changing the order: z x y 0 4 5 4 1 1 2 1 2 5 1 5 3 0 9 10
- Related Articles
- How to sort multiple columns of a Pandas DataFrame?
- Python - Renaming the columns of Pandas DataFrame
- Change Data Type for one or more columns in Pandas Dataframe
- Change Data Type for one or more columns in Pandas Dataframe
- Python Pandas - Query the columns of a DataFrame
- How to change the DPI of a Pandas Dataframe Plot in Matplotlib?
- Python - Reverse the column order of the Pandas DataFrame
- How to change the order of plots in Pandas hist command?
- Change the order of the Bootstrap grid columns
- Python - Grouping columns in Pandas Dataframe
- How to change the order of columns in an R data frame?
- Plot multiple columns of Pandas DataFrame using Seaborn
- How to select all columns except one in a Pandas DataFrame?
- Select multiple columns in a Pandas DataFrame
- How to find the standard deviation of specific columns in a dataframe in Pandas Python?

Advertisements