0
0
Pandasdata~5 mins

Why indexing matters in Pandas

Choose your learning style9 modes available
Introduction

Indexing helps you find and organize data quickly. It makes working with tables easier and faster.

When you want to select specific rows or columns from a table.
When you need to join two tables based on a common column.
When you want to sort data or remove duplicates efficiently.
When you want to label rows with meaningful names instead of numbers.
When you want to speed up searching for data in large tables.
Syntax
Pandas
df.set_index('column_name')
df.loc[index_label]
df.iloc[row_number]

set_index changes which column is used to label rows.

loc selects rows by their label, iloc selects by position.

Examples
This sets the 'Name' column as the index, so rows are labeled by names.
Pandas
df = pd.DataFrame({'Name': ['Anna', 'Bob'], 'Age': [25, 30]})
df.set_index('Name')
Selects the row where the index label is 'Anna'.
Pandas
df.loc['Anna']
Selects the second row by position (counting starts at 0).
Pandas
df.iloc[1]
Sample Program

This program shows how setting the 'City' column as the index helps us select data by city name easily.

Pandas
import pandas as pd

# Create a simple table
data = {'City': ['Paris', 'London', 'Berlin'], 'Population': [2148000, 8982000, 3769000]}
df = pd.DataFrame(data)

# Set 'City' as the index
df_indexed = df.set_index('City')

# Select population of London using index label
london_pop = df_indexed.loc['London', 'Population']

print('Table with City as index:')
print(df_indexed)
print(f"Population of London: {london_pop}")
OutputSuccess
Important Notes

Indexes can be simple (one column) or multi-level (multiple columns).

Using meaningful indexes makes data easier to read and faster to access.

Remember to reset the index if you want to go back to default numbering.

Summary

Indexing labels rows to help find data quickly.

Use set_index to choose which column labels rows.

loc and iloc help select data by label or position.