+4 votes
in Programming Languages by (74.2k points)
I want to change the index of a dataframe. The current values of the index are 0, 1, 2,... (the default value). I want to set it to some of the existing columns of the dataframe. How can I do it?

1 Answer

+2 votes
by (349k points)
selected by
 
Best answer

You can use the set_index() function of the pandas dataframe. This function sets the DataFrame index (row labels) using one or more existing columns of the DataFrame. The index can replace the existing index or expand on it.

Here is an example:

>>> import pandas as pd
>>> df=pd.DataFrame({'A':[1,2,3,4,5], 'B':[10,20,30,40,50], 'C':[11,12,13,14,15]})
>>> df
   A   B   C
0  1  10  11
1  2  20  12
2  3  30  13
3  4  40  14
4  5  50  15

Using one column

>>> df.set_index('C')
    A   B
C        
11  1  10
12  2  20
13  3  30
14  4  40
15  5  50

Using multiple columns

>>> df.set_index(['C','A'])
       B
C  A    
11 1  10
12 2  20
13 3  30
14 4  40
15 5  50

Expand the existing index

>>> df.set_index('C', append=True)
      A   B
  C        
0 11  1  10
1 12  2  20
2 13  3  30
3 14  4  40
4 15  5  50


...