Pandas: How to Set Column as Index


You can use the following syntax to set a column in a pandas DataFrame as the index:

#set one column as index
df.set_index('col1')

#set multiple columns as multi index
df.set_index(['col1', 'col2'])

The following examples show how to use this syntax in practice with the following DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'points': [5, 7, 7, 9, 12, 9],
                   'assists': [11, 8, 10, 6, 6, 5],
                   'team': ['A', 'B', 'C', 'D', 'E', 'F'],
                   'conference': [1, 2, 3, 4, 5, 6]})

#view DataFrame
df

	points	assists	team	conference
0	5	11	A	1
1	7	8	B	2
2	7	10	C	3
3	9	6	D	4
4	12	6	E	5
5	9	5	F	6

Example 1: Set One Column as Index

The following code shows how to set one column of the pandas DataFrame as the index:

df.set_index('team')

	points	assists	conference
team			
A	5	11	1
B	7	8	2
C	7	10	3
D	9	6	4
E	12	6	5
F	9	5	6

Example 2: Set Multiple Columns as Index

The following code shows how to set multiple columns of the pandas DataFrame as a Multi-Index::

df.set_index(['team', 'conference'])

		   points  assists
team	conference		
A	1	   5	   11
B	2	   7	   8
C	3	   7	   10
D	4	   9	   6
E	5	   12	   6
F	6	   9	   5

Additional Resources

How to Rename Index in Pandas DataFrame
How to Drop Rows by Index in Pandas
How to Drop Columns by Index in Pandas

Leave a Reply

Your email address will not be published.