Recently, I was working on a data analysis project where I needed to extract and manipulate index values from a Pandas DataFrame. As I dug into the problem, I realized that accessing index values isn’t always as simple as it seems.
Pandas provides several useful methods to retrieve index values, but knowing which approach to use in different situations can be challenging for many developers.
In this article, I will walk you through various methods to get index values from DataFrames in Pandas Python.
So let us start.
Methods to Get Index Values from DataFrames in Pandas Python
Let me show you some important methods to get index values from DataFrames in pandas Python.
1 – Use the .index Attribute
The simplest way to access the index of a DataFrame is by using the .index attribute in Python. This returns the complete index object, which you can then convert to other data types as needed.
Let’s start with a simple example:
import pandas as pd
# Create a sample DataFrame with sales data from different US states
data = {
'State': ['California', 'Texas', 'Florida', 'New York', 'Illinois'],
'Sales': [120000, 98000, 105000, 115000, 89000],
'Year': [2022, 2022, 2022, 2022, 2022]
}
df = pd.DataFrame(data)
print(df)
# Access and print the index
print("\nIndex values:")
print(df.index)
# Convert and print the index as a list
print("\nIndex as list:")
print(list(df.index))Output:
State Sales Year
0 California 120000 2022
1 Texas 98000 2022
2 Florida 105000 2022
3 New York 115000 2022
4 Illinois 89000 2022
Index values:
RangeIndex(start=0, stop=5, step=1)
Index as list:
[0, 1, 2, 3, 4]I executed the above example code and added the screenshot below.

When you run this code, you’ll see that the .index attribute returns a RangeIndex object. Converting it to a list gives us the integer indices of the DataFrame.
2 – Use index.get_level_values()
When working with multi-level indices (hierarchical indices), the Python get_level_values() method becomes extremely useful. It allows you to extract the values of a specific level in the index.
Here’s how to use it:
# Create a DataFrame with a multi-level index
# US sales data by state and quarter
states = ['California', 'California', 'Texas', 'Texas', 'Florida', 'Florida']
quarters = ['Q1', 'Q2', 'Q1', 'Q2', 'Q1', 'Q2']
sales = [32000, 38000, 24000, 26000, 27000, 29000]
multi_df = pd.DataFrame({'Sales': sales}, index=[states, quarters])
multi_df.index.names = ['State', 'Quarter']
print(multi_df)
# Get values from the first level (States)
state_values = multi_df.index.get_level_values(0)
print("\nStates in the index:")
print(state_values)
# Get values from the second level (Quarters)
quarter_values = multi_df.index.get_level_values('Quarter')
print("\nQuarters in the index:")
print(quarter_values)Output:
Sales
State Quarter
California Q1 32000
Q2 38000
Texas Q1 24000
Q2 26000
Florida Q1 27000
Q2 29000
States in the index:
Index(['California', 'California', 'Texas', 'Texas', 'Florida', 'Florida'], dtype='object', name='State')
Quarters in the index:
Index(['Q1', 'Q2', 'Q1', 'Q2', 'Q1', 'Q2'], dtype='object', name='Quarter')I executed the above example code and added the screenshot below.

As you can see, we can access the level either by its position (0, 1, etc.) or by its name (‘State’, ‘Quarter’). This flexibility makes the method extremely useful for real-world data analysis.
Read Python Pandas Write to Excel
3 – Find Indices of Specific Values
Often, you’ll need to find the index positions where certain values appear in your DataFrame. Let’s look at how to do this:
# Create a DataFrame with US stock price data
stock_data = {
'Stock': ['AAPL', 'MSFT', 'GOOGL', 'AMZN', 'TSLA', 'AAPL', 'MSFT'],
'Price': [180.5, 340.2, 132.8, 145.6, 246.9, 182.3, 338.7],
'Date': ['2023-05-01', '2023-05-01', '2023-05-01',
'2023-05-01', '2023-05-01', '2023-05-02', '2023-05-02']
}
stock_df = pd.DataFrame(stock_data)
print(stock_df)
# Find indices where Stock is 'AAPL'
apple_indices = stock_df.index[stock_df['Stock'] == 'AAPL'].tolist()
print("\nIndices where Stock is 'AAPL':")
print(apple_indices)
# Find indices where Price > 200
high_price_indices = stock_df.index[stock_df['Price'] > 200].tolist()
print("\nIndices where Price > 200:")
print(high_price_indices)Output:
Stock Price Date
0 AAPL 180.5 2023-05-01
1 MSFT 340.2 2023-05-01
2 GOOGL 132.8 2023-05-01
3 AMZN 145.6 2023-05-01
4 TSLA 246.9 2023-05-01
5 AAPL 182.3 2023-05-02
6 MSFT 338.7 2023-05-02
Indices where Stock is 'AAPL':
[0, 5]
Indices where Price > 200:
[1, 4, 6]I executed the above example code and added the screenshot below.

This method combines Boolean indexing with the .index attribute to find positions meeting specific criteria, which is incredibly useful for filtering and analysis.
4 – Use get_indexer() for Value Lookup
Python get_indexer() method allows you to find the indices of specific values in the index itself:
# Create a DataFrame with named index
employee_data = {
'Department': ['Engineering', 'Marketing', 'Finance', 'HR', 'Sales'],
'Salary': [120000, 85000, 95000, 75000, 90000],
'Years': [5, 3, 7, 2, 4]
}
employee_df = pd.DataFrame(employee_data)
employee_df.set_index('Department', inplace=True)
print(employee_df)
# Get the index object
idx = employee_df.index
# Find positions of specific departments
positions = idx.get_indexer(['Marketing', 'Sales', 'IT'])
print("\nPositions of departments:")
print(positions) # Note: returns -1 for 'IT' as it doesn't exist in the indexThe get_indexer() method returns the integer positions of values in the index. If a value doesn’t exist in the index, it returns -1, which is a helpful feature for detecting missing entries.
5 – Work with DatetimeIndex
When working with time series data, Pandas provides specialized methods for accessing date-based indices:
# Create a DataFrame with time series data (US retail sales)
dates = pd.date_range(start='2023-01-01', periods=6, freq='M')
retail_sales = [5.2, 5.4, 5.7, 5.9, 6.1, 6.3] # in billions of dollars
ts_df = pd.DataFrame({'Sales': retail_sales}, index=dates)
print(ts_df)
# Get the year values from the index
years = ts_df.index.year
print("\nYears in the index:")
print(years)
# Get the month values from the index
months = ts_df.index.month
print("\nMonths in the index:")
print(months)
# Get dates that fall on a specific month
march_indices = ts_df.index[ts_df.index.month == 3]
print("\nMarch dates in the index:")
print(march_indices)DatetimeIndex objects provide a wealth of attributes for extracting date components like year, month, day, etc., making time series analysis much more intuitive.
Check out Create Plots Using Pandas crosstab() in Python
6 – Reset and Setting Indices
Sometimes, you may want to convert an index to a column or vice versa:
# Create a DataFrame with US university rankings
university_data = {
'University': ['Harvard', 'Stanford', 'MIT', 'Princeton', 'Yale'],
'Ranking': [1, 2, 3, 4, 5],
'State': ['MA', 'CA', 'MA', 'NJ', 'CT']
}
univ_df = pd.DataFrame(university_data)
print(univ_df)
# Set 'University' as the index
univ_df.set_index('University', inplace=True)
print("\nDataFrame with 'University' as index:")
print(univ_df)
# Get the index values
universities = univ_df.index.tolist()
print("\nUniversities in the index:")
print(universities)
# Reset the index back to default
univ_df_reset = univ_df.reset_index()
print("\nDataFrame with reset index:")
print(univ_df_reset)The set_index() and reset_index() methods are crucial for restructuring your DataFrame, especially when preparing data for specific visualization or analysis tasks.
Find Unique Values in an Index
When dealing with datasets that might have duplicate index values, you can extract just the unique ones:
# Create a DataFrame with some duplicate index values
# US customer data
customer_ids = [101, 102, 103, 101, 104, 102]
purchases = [250, 320, 180, 420, 510, 280]
customer_df = pd.DataFrame({'Purchase': purchases}, index=customer_ids)
customer_df.index.name = 'CustomerID'
print(customer_df)
# Get unique index values
unique_customers = customer_df.index.unique()
print("\nUnique customer IDs:")
print(unique_customers)The unique() method is particularly helpful when you need to identify distinct categories or entities in your dataset without repetition.
I’ve found these methods incredibly useful in my data analysis projects. They’ve helped me efficiently extract, filter, and manipulate index values in various scenarios, from financial data analysis to customer behavior tracking.
I hope you found these techniques helpful for your data analysis projects.
Other Python Pandas articles you may also like:
- Pandas Merge Fill NAN with 0 in Python
- Pandas Find Duplicates in Python
- Pandas str.replace Multiple Values in Python

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.