change the order of columns in pandas | sort columns in pandas | reindex in pandas
In this pandas tutorial, we will discuss several ways to:
-
reindex in pandas,
-
change the order of columns in pandas,
-
sort columns in pandas dataframe,
First lets go through the concept of creating and understanding pandas dataframe. Later we will check how to reindex columns in pandas dataframe.
DataFrame is an two dimensional data structure that will store data in two dimensional format. One dimension refers to a row and second dimension refers to a column, So It will store the data in rows and columns.
We can create this DataFrame using DataFrame() method. But this is available in pandas module, so we have to import pandas module.
Syntax:
pandas.DataFrame(data)
Where, data is the input dataframe , The data can be a dictionary that stores list of values with specified key.
Example: Create Pandas dataframe
In this example, we will create a dataframe with 4 rows and 4 columns with college data and assign indices through index parameter.
import pandas as pd
#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],
'college_name':["vignan university","vvit","RVR - JC","Andhra University"],
"college_address":["guntur","guntur","guntur","guntur"],
"Total Staff":['1200','3422','5644','670']
},index=['one','two','three','four'])
#display the dataframe
print(data)
Output: DataFrame is created below
college_id college_name college_address Total Staff
one c-001 vignan university guntur 1200
two c-021 vvit guntur 3422
three c-002 RVR - JC guntur 5644
four c-004 Andhra University guntur 670
Now lets talk about different methods to change the order of columns in pandas.
Method 1 : change the order of columns in pandas using reindex()
reindex in pandas is used to change the order of columns by taking the column names as input in specified order.
So, it will take only one parameter. i.e columns in a list.
Syntax:
dataframe.reindex(columns=[columns....])
where,
1. dataframe is the input dataframe
2. columns is the parameter which will take list of columns to be arranged in the order.
Example: reindex columns in pandas dataframe using reindex()
In this reindex columns in pandas dataframe example, we will change the order of columns.
import pandas as pd
from tabulate import tabulate
#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],
'college_name':["vignan university","vvit","RVR - JC","Andhra University"],
"college_address":["guntur","guntur","guntur","guntur"],
"Total Staff":['1200','3422','5644','670']
},index=['one','two','three','four'])
# change the column order
data = data.reindex(columns=['college_address','college_name','college_id','Total Staff'])
#display the dataframe
print(data)
Output: After we reindex columns in pandas dataframe below result is obtained
We will change the order of columns in the following order - ['college_address','college_name','college_id','Total Staff']
college_address college_name college_id Total Staff
one guntur vignan university c-001 1200
two guntur vvit c-021 3422
three guntur RVR - JC c-002 5644
four guntur Andhra University c-004 670
In the above code, we arranged the columns of the dataframe in the order - 'college_address','college_name','college_id','Total Staff' and display the entire dataframe.
sort columns in pandas dataframe
Using the above reindex in pandas method we can also change the order of columns alphabetically.
By passing sorted() method as a parameter in reindex() method, we can change the order.
Syntax:
dataframe.reindex(sorted(dataframe.columns), axis=1)
where,
1. dataframe is the input dataframe
2. sorted(dataframe.columns) is the method used to order the columns by taking columns method.
3. axis =1 specifies column.
Example: sort columns in pandas dataframe
Given below is code snippet that sort columns in pandas dataframe or change the order of columns alphabetically.
import pandas as pd
from tabulate import tabulate
#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],
'college_name':["vignan university","vvit","RVR - JC","Andhra University"],
"college_address":["guntur","guntur","guntur","guntur"],
"Total Staff":['1200','3422','5644','670']
},index=['one','two','three','four'])
# change the column order alphabetically
data = data.reindex(sorted(data.columns), axis=1)
#display the dataframe
print(data)
Output: sort columns in pandas dataframe alphabetically result
Total Staff college_address college_id college_name
one 1200 guntur c-001 vignan university
two 3422 guntur c-021 vvit
three 5644 guntur c-002 RVR - JC
four 670 guntur c-004 Andhra University
In the above code, we change the column order alphabetically
So the order will be Total Staff, college_address, college_id, college_name
This way we can change the order of columns in pandas and sort columns in pandas using reindex in pandas.
Would you like to see your article here on tutorialsinhand.
Join
Write4Us program by tutorialsinhand.com
About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University.
Published 1400+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :
Published Date :
Mar 17,2022