Pandas - DataFrame - Index

By xngo on March 5, 2020

Access index

To access DataFrame data column, you would call it with the column name. However, for DataFrame index, it is different. You have to use its index keyword. Here are the examples.

#!/usr/bin/python3
from io import StringIO
import pandas as pd
 
data="""Date,Stock,Open,High,Low,Close,Volume
2016-09-29,KESM,7.92,7.98,7.92,7.97,149400
2016-09-30,KESM,7.96,7.97,7.84,7.9,29900
2016-10-04,KESM,7.8,7.94,7.8,7.93,99900
2016-10-05,KESM,7.93,7.95,7.89,7.93,77500
2016-10-06,KESM,7.93,7.93,7.89,7.92,130600
2016-10-07,KESM,7.91,7.94,7.91,7.92,103000"""
 
# Load data and set Date as index column.
df = pd.read_csv(StringIO(data), index_col='Date', parse_dates=True)
 
# Print index name.
print(df.index.name)
 
# Print index value at specific position.
print(df.index[3])
 
# Print index values.
print(df.index)
Date
2016-10-05 00:00:00
DatetimeIndex(['2016-09-29', '2016-09-30', '2016-10-04', '2016-10-05',
               '2016-10-06', '2016-10-07'],
              dtype='datetime64[ns]', name='Date', freq=None)

Change DataFrame index

You can change the DataFrame index to use another column by using the set_index() function.

However, before you do this, it is important to remember to reset your index to expose your current index as data column. Otherwise, your current index data will be lost. For the example below, I will change the index column from Date column to Stock column.

# ...
 
# Reset index to preserve 'Date' data.
df.reset_index(inplace = True)
 
# Change DataFrame index to 'Stock'.
df.set_index(['Stock'], drop=True, append=False, inplace=True)
 
# Print outcome.
print(df)
            Date  Open  High   Low  Close  Volume
Stock                                            
KESM  2016-09-29  7.92  7.98  7.92   7.97  149400
KESM  2016-09-30  7.96  7.97  7.84   7.90   29900
KESM  2016-10-04  7.80  7.94  7.80   7.93   99900
KESM  2016-10-05  7.93  7.95  7.89   7.93   77500
KESM  2016-10-06  7.93  7.93  7.89   7.92  130600
KESM  2016-10-07  7.91  7.94  7.91   7.92  103000

Set multiple columns as index

To set multiple columns as index, simply list all your desired columns in the keys parameter of the set_index() function.

# ...
 
# Set multiple columns as index: 'Date', 'Stock'.
df.set_index(['Date', 'Stock'], drop=True, append=False, inplace=True)
 
# Print outcome.
print(df)
                  Open  High   Low  Close  Volume
Date       Stock                                 
2016-09-29 KESM   7.92  7.98  7.92   7.97  149400
2016-09-30 KESM   7.96  7.97  7.84   7.90   29900
2016-10-04 KESM   7.80  7.94  7.80   7.93   99900
2016-10-05 KESM   7.93  7.95  7.89   7.93   77500
2016-10-06 KESM   7.93  7.93  7.89   7.92  130600
2016-10-07 KESM   7.91  7.94  7.91   7.92  103000

Github

  • https://github.com/xuanngo2001/python-examples/blob/master/pandas/pandas-dataframe-index.py
  • https://github.com/xuanngo2001/python-examples/blob/master/pandas/pandas-dataframe-index-set.py
  • https://github.com/xuanngo2001/python-examples/blob/master/pandas/pandas-dataframe-index-multiple.py

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.