pandas DataFrame set_axis() and set_index()
In this pandas tutorial we will discuss about methods:
-
set_axis() or set axis in pandas and
-
set_index() or set index in pandas.
Introduction
DataFrame in pandas 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 able to 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 DataFrame
In this example, we will create dataframe with 4 rows and 4 columns with college data
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]
})
#display the dataframe
print(data)
Output: DataFrame is created below.
college_id college_name college_address Total Staff
0 c-001 vignan university guntur 1200
1 c-021 vvit guntur 3422
2 c-002 RVR - JC guntur 5644
3 c-004 Andhra University guntur 670
set_axis() or set axis in pandas
set_axis() or set axis in pandas is used to assign the indices for rows/columns.
Syntax:
dataframe_input.set_axis(labels, axis)
where,
-
dataframe_input is the input dataframe
-
labels is the names for rows/columns taken through a list.
-
axis = 0 specifies row indices and axis=1 specify column indices
Example 1: set axis in pandas example - row indices
For the above dataframe, we will assign the row indices as ['A','B','C','D'].
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]
})
#display the dataframe by adding row indices
print(data.set_axis(['A','B','C','D'],axis=0))
Output:
In the above code , we added labels to the rows - ['A','B','C','D'].
college_id college_name college_address Total Staff
A c-001 vignan university guntur 1200
B c-021 vvit guntur 3422
C c-002 RVR - JC guntur 5644
D c-004 Andhra University guntur 670
Example 2: set axis in pandas example - column indices
For the above dataframe, we will assign the column indices as ['A','B','C','D'].
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]
})
#display the dataframe by adding column indices
print(data.set_axis(['A','B','C','D'],axis=1))
Output:
In the above code , we added labels to the column - ['A','B','C','D'].
A B C D
0 c-001 vignan university guntur 1200
1 c-021 vvit guntur 3422
2 c-002 RVR - JC guntur 5644
3 c-004 Andhra University guntur 670
set_index() or set index in pandas
This set index in pandas method is used to set the dataframe index with the existing column/s.
Syntax:
For single column:
dataframe_input.set_index('column_name')
For multiple columns:
dataframe_input.set_index(['columns'])
where,
-
dataframe_input is the input dataframe
-
column_name is the name of the column which act as index to the dataframe
Example 1: set index in pandas - column indices
In this example, we will assign college_address column as index.
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]
})
#display the dataframe by setting college_address column as index
print(data.set_index('college_address'))
Output:
So for this dataframe 'college_address' column will be the index.
college_id college_name Total Staff
college_address
guntur c-001 vignan university 1200
guntur c-021 vvit 3422
guntur c-002 RVR - JC 5644
guntur c-004 Andhra University 670
Example 2: set index in pandas - multiple column indices
In this example, we will assign college_address,college_name and college_id columns as index.
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]
})
print(data.set_index(['college_address','college_name','college_address']))
Output:
So for this dataframe college_address,college_name and college_id columns will be the index.
college_id Total Staff
college_address college_name college_address
guntur vignan university guntur c-001 1200
vvit guntur c-021 3422
RVR - JC guntur c-002 5644
Andhra University guntur c-004 670
This wraps up our session on set axis in pandas and set index 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 :
May 11,2023