15.1 C
London
Friday, July 5, 2024
HomePandas in PythonDataFrame Functions in PythonPandas: How to Set Column as Index

Pandas: How to Set Column as Index

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories