How do you change the index of a column in python?

DataFrame.set_index(keys, drop=True, append=False, inplace=False, verify_integrity=False)[source]#

Set the DataFrame index using existing columns.

Set the DataFrame index (row labels) using one or more existing columns or arrays (of the correct length). The index can replace the existing index or expand on it.

Parameterskeyslabel or array-like or list of labels/arrays

This parameter can be either a single column key, a single array of the same length as the calling DataFrame, or a list containing an arbitrary combination of column keys and arrays. Here, “array” encompasses Series, Index, np.ndarray, and instances of Iterator.

dropbool, default True

Delete columns to be used as the new index.

appendbool, default False

Whether to append columns to existing index.

inplacebool, default False

Whether to modify the DataFrame rather than creating a new one.

verify_integritybool, default False

Check the new index for duplicates. Otherwise defer the check until necessary. Setting to False will improve the performance of this method.

ReturnsDataFrame or None

Changed row labels or None if inplace=True.

Examples

>>> df = pd.DataFrame({'month': [1, 4, 7, 10],
...                    'year': [2012, 2014, 2013, 2014],
...                    'sale': [55, 40, 84, 31]})
>>> df
   month  year  sale
0      1  2012    55
1      4  2014    40
2      7  2013    84
3     10  2014    31

Set the index to become the ‘month’ column:

>>> df.set_index('month')
       year  sale
month
1      2012    55
4      2014    40
7      2013    84
10     2014    31

Create a MultiIndex using columns ‘year’ and ‘month’:

>>> df.set_index(['year', 'month'])
            sale
year  month
2012  1     55
2014  4     40
2013  7     84
2014  10    31

Create a MultiIndex using an Index and a column:

>>> df.set_index([pd.Index([1, 2, 3, 4]), 'year'])
         month  sale
   year
1  2012  1      55
2  2014  4      40
3  2013  7      84
4  2014  10     31

Create a MultiIndex using two Series:

>>> s = pd.Series([1, 2, 3, 4])
>>> df.set_index([s, s**2])
      month  year  sale
1 1       1  2012    55
2 4       4  2014    40
3 9       7  2013    84
4 16     10  2014    31

Contents

  • Introduction
  • Syntax of set_index()
  • Example 1: Set Column as Index in Pandas DataFrame
  • Example 2: Set MultiIndex for Pandas DataFrame
  • Summary

By default an index is created for DataFrame. But, you can set a specific column of DataFrame as index, if required.

To set a column as index for a DataFrame, use DataFrame.set_index() function, with the column name passed as argument.

You can also setup MultiIndex with multiple columns in the index. In this case, pass the array of column names required for index, to set_index() method.

Syntax of set_index()

The syntax of set_index() to setup a column as index is

myDataFrame.set_index('column_name')

where myDataFrame is the DataFrame for which you would like to set column_name column as index.

To setup MultiIndex, use the following syntax.

myDataFrame.set_index(['column_name_1', column_name_2])

Run

You can pass as many column names as required.

Note that set_index() method does not modify the original DataFrame, but returns the DataFrame with the column set as index.

Example 1: Set Column as Index in Pandas DataFrame

In this example, we take a DataFrame, and try to set a column as index.

Python Program

import pandas as pd

#initialize a dataframe
df = pd.DataFrame(
	[[21, 'Amol', 72, 67],
	[23, 'Lini', 78, 69],
	[32, 'Kiku', 74, 56],
	[52, 'Ajit', 54, 76]],
	columns=['rollno', 'name', 'physics', 'botony'])

print('DataFrame with default index\n', df)

#set column as index
df = df.set_index('rollno')

print('\nDataFrame with column as index\n',df)

Run

Output

How do you change the index of a column in python?

The column rollno of the DataFrame is set as index.

Also, observe the output of original dataframe and the output of dataframe with rollno as index. In the original dataframe, there is a separate index column (first column) with no column name. But in our second dataframe, as existing column is acting as index, this column took the first place.

Example 2: Set MultiIndex for Pandas DataFrame

In this example, we will pass multiple column names as an array to set_index() method to setup MultiIndex for the Pandas DataFrame.

Python Program

import pandas as pd

#initialize a dataframe
df = pd.DataFrame(
	[[21, 'Amol', 72, 67],
	[23, 'Lini', 78, 69],
	[32, 'Kiku', 74, 56],
	[52, 'Ajit', 54, 76]],
	columns=['rollno', 'name', 'physics', 'botony'])

print('DataFrame with default index\n', df)

#set multiple columns as index
df = df.set_index(['rollno','name'])

print('\nDataFrame with MultiIndex\n',df)

Run

Output

D:\>python example1.py
DataFrame with default index
    rollno  name  physics  botony
0      21  Amol       72      67
1      23  Lini       78      69
2      32  Kiku       74      56
3      52  Ajit       54      76

DataFrame with MultiIndex
              physics  botony
rollno name
21     Amol       72      67
23     Lini       78      69
32     Kiku       74      56
52     Ajit       54      76

Summary

In this Pandas Tutorial, we learned how to set a specific column of the DataFrame as index.

  • How to Reset Index of Pandas DataFrame?
  • How to Get Index of Pandas DataFrame?
  • How to Get Specific Row in Pandas DataFrame using Index?
  • Pandas DataFrame – Sort by Index

How do you modify an index in Python?

To change the index values we need to use the set_index method which is available in pandas allows specifying the indexes..
inplace parameter accepts True or False, which specifies that change in index is permanent or temporary..
True indicates that change is Permanent..
False indicates that the change is Temporary..

How do you rename a column index in Python?

You can use the rename() method of pandas. DataFrame to change column/index name individually. Specify the original name and the new name in dict like {original name: new name} to columns / index parameter of rename() . columns is for the column name, and index is for the index name.

Can we change index in DataFrame?

Pandas DataFrame: set_index() function The set_index() function is used to set the DataFrame index using existing columns. Set the DataFrame index (row labels) using one or more existing columns or arrays of the correct length. The index can replace the existing index or expand on it.

How do you create an index of a column in a DataFrame in Python?

In order to set index to column in pandas DataFrame use reset_index() method. By using this you can also set single, multiple indexes to a column. If you are not aware by default, pandas adds an index to each row of the pandas DataFrame.