Indexing in Pandas

Last Updated: 29th August 2025


Indexing is a way to pick specific rows and columns from a DataFrame or Series.Always avoid chain indexing. There are 5 common ways:


Let Data is like this

import pandas as pd

df = pd.DataFrame({
  "Name": ["Amit", "Ravi", "Neha"],
  "Age": [25, 30, 22],
  "Marks": [85, 90, 95]
})

1 [] Basic indexing:-

  • Works mainly for columns in DataFrame and positions/labels in Series.
# Select a column
print(df["Name"])

# Multiple columns
print(df[["Name", "Marks"]])

2 .loc[] Label-based indexing:-

  • Select by row label and column label.
# Row by label
print(df.loc[0])         # First row (label 0)

# Row + specific column
print(df.loc[0, "Name"])  # "Amit"

# Multiple rows + columns
print(df.loc[0:1, ["Name", "Marks"]])

# Chain indexing for get a single value
print(df[df["Age"]>20]["Marks"] = 90)  # Avoid it

# Use loc[]
print(df.loc[df["Age"]>20, "Marks"] = 90)

3 .iloc[] Integer-based indexing:-

  • Select by row number and column number.
# Row by index number
print(df.iloc[0])        # First row

# Row + specific column
print(df.iloc[0, 1])     # First row, second column → 25

# Multiple rows + columns
print(df.iloc[0:2, 0:2])

4 .at[] Label-based indexing for single value

  • Get single value by row label + column name.
  • Faster than .loc[].
print(df.at[0, "Name"])  # "Amit"

5 .iat[] Integer-based indexing for single value

  • Get single value by row number + column number.
  • Faster than .iloc[].
print(df.iat[0, 1])  # 25