How to Use Index in pandas: Syntax, Examples, and Tips
In pandas, the
index is used to label rows in a DataFrame or Series. You can access it with .index, set it with .set_index(), or reset it with .reset_index() to organize and reference your data efficiently.Syntax
The main ways to work with index in pandas are:
df.index: Access the index labels of a DataFrame.df.set_index(column_name): Set a column as the new index.df.reset_index(): Reset the index to default integer index.
These let you view, change, or reset the row labels.
python
import pandas as pd data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35]} df = pd.DataFrame(data) # Access index print(df.index) # Set 'Name' column as index df2 = df.set_index('Name') print(df2.index) # Reset index df3 = df2.reset_index() print(df3.index)
Output
RangeIndex(start=0, stop=3, step=1)
Index(['Alice', 'Bob', 'Charlie'], dtype='object', name='Name')
RangeIndex(start=0, stop=3, step=1)
Example
This example shows how to set a column as the index, access it, and reset it back to default.
python
import pandas as pd data = {'City': ['Paris', 'London', 'Berlin'], 'Population': [2148000, 8982000, 3769000]} df = pd.DataFrame(data) # Original DataFrame print('Original DataFrame:') print(df) # Set 'City' as index df_indexed = df.set_index('City') print('\nDataFrame with City as index:') print(df_indexed) # Access index labels print('\nIndex labels:') print(df_indexed.index) # Reset index to default reset_df = df_indexed.reset_index() print('\nDataFrame after resetting index:') print(reset_df)
Output
Original DataFrame:
City Population
0 Paris 2148000
1 London 8982000
2 Berlin 3769000
DataFrame with City as index:
Population
City
Paris 2148000
London 8982000
Berlin 3769000
Index labels:
Index(['Paris', 'London', 'Berlin'], dtype='object', name='City')
DataFrame after resetting index:
City Population
0 Paris 2148000
1 London 8982000
2 Berlin 3769000
Common Pitfalls
Common mistakes when using index in pandas include:
- Forgetting to assign the result of
set_index()back to a variable, since it returns a new DataFrame. - Using
reset_index()withoutdrop=Truewhen you want to remove the old index completely. - Assuming the index is always unique; duplicate index labels can cause unexpected behavior.
python
import pandas as pd data = {'ID': [1, 2, 3], 'Value': [10, 20, 30]} df = pd.DataFrame(data) # Wrong: set_index without assignment (does not change df) df.set_index('ID') print('Index after set_index without assignment:') print(df.index) # Right: assign back to df df = df.set_index('ID') print('\nIndex after set_index with assignment:') print(df.index) # Reset index but keep old index as column reset_df = df.reset_index() print('\nDataFrame after reset_index without drop:') print(reset_df) # Reset index and drop old index reset_df_drop = df.reset_index(drop=True) print('\nDataFrame after reset_index with drop=True:') print(reset_df_drop)
Output
Index after set_index without assignment:
RangeIndex(start=0, stop=3, step=1)
Index after set_index with assignment:
Index([1, 2, 3], dtype='int64', name='ID')
DataFrame after reset_index without drop:
ID Value
0 1 10
1 2 20
2 3 30
DataFrame after reset_index with drop=True:
Value
0 10
1 20
2 30
Quick Reference
Here is a quick summary of common index operations in pandas:
| Operation | Code Example | Description |
|---|---|---|
| Access index | df.index | Get the index labels of the DataFrame or Series |
| Set index | df.set_index('column') | Make a column the new index (returns new DataFrame) |
| Reset index | df.reset_index() | Reset index to default integer index, old index becomes column |
| Reset index and drop | df.reset_index(drop=True) | Reset index and remove old index completely |
| Check if index is unique | df.index.is_unique | Returns True if index labels are unique |
Key Takeaways
Use
.index to view the row labels of a DataFrame or Series.Use
.set_index() to set a column as the index, but assign the result to keep changes.Use
.reset_index() to revert to default integer index; add drop=True to remove old index.Index labels can be any hashable type and do not have to be unique, but unique indexes avoid confusion.
Always check your index after operations to avoid unexpected data alignment issues.