🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpandas

pandas Documentation

LOADING ENGINE...

df.iloc[]

AI & DATA SCIENCE // df-iloc

df.iloc[] selects rows and columns by integer position, the same 0-based, exclusive-end convention as Python list indexing and slicing.

Syntax

df.iloc[row_positions, column_positions]

Deep Dive Course

iloc[] is purely positional — selecting position 0 returns the first row regardless of what its actual index label is, selecting column position 1 selects the second column by position, and slicing from position 0 to 2 selects the first two rows, excluding position 2, matching ordinary Python slicing semantics. Since iloc[] ignores labels entirely, it behaves identically whether the DataFrame's index is the default RangeIndex or a custom one, which makes it the right tool when you specifically want positional access independent of whatever labels happen to be in use.

1Understanding df.iloc[]

iloc[] is purely positional — selecting position 0 returns the first row regardless of what its actual index label is, selecting column position 1 selects the second column by position, and slicing from position 0 to 2 selects the first two rows, excluding position 2, matching ordinary Python slicing semantics. Since iloc[] ignores labels entirely, it behaves identically whether the DataFrame's index is the default RangeIndex or a custom one, which makes it the right tool when you specifically want positional access independent of whatever labels happen to be in use.

💡

Use iloc[] when you specifically want positional access that behaves the same regardless of the DataFrame's actual index labels — loc[] instead depends entirely on what those labels are, which changes behavior if the index is reset, reordered, or customized.

editor.html
import pandas as pd

df = pd.DataFrame({"name": ["Alice", "Bob", "Carol"]}, index=["x", "y", "z"])
print(df.iloc[0])
localhost:3000

2Practical Example

Here is a real-world application of df.iloc[] showing how it is used in production Pandas code.

editor.html
import pandas as pd

df = pd.DataFrame({"a": [1, 2, 3, 4], "b": [5, 6, 7, 8]})
print(df.iloc[1:3, 0])
localhost:3000

3Best Practices

Follow these guidelines when working with df.iloc[]:

1. Use iloc[] for purely positional access, like getting the first 10 rows, that should work the same regardless of the index's actual labels

2. Use loc[] instead when the selection is naturally based on meaningful labels rather than position

3. Remember iloc[]'s slice endpoint is exclusive, matching ordinary Python slicing, unlike loc[]'s inclusive label-based slicing

⚠️

Tip: Use iloc[] when you specifically want positional access that behaves the same regardless of the DataFrame's actual index labels — loc[] instead depends entirely on what those labels are, which changes behavior if the index is reset, reordered, or customized.

editor.html
import pandas as pd

df = pd.DataFrame({"name": ["Alice", "Bob", "Carol"]}, index=["x", "y", "z"])
print(df.iloc[0])
localhost:3000

Examples

Example 01Basic Usage
import pandas as pd

df = pd.DataFrame({"name": ["Alice", "Bob", "Carol"]}, index=["x", "y", "z"])
print(df.iloc[0])
Example 02Advanced Example
import pandas as pd

df = pd.DataFrame({"a": [1, 2, 3, 4], "b": [5, 6, 7, 8]})
print(df.iloc[1:3, 0])

Best Practices

  • Use iloc[] for purely positional access, like getting the first 10 rows, that should work the same regardless of the index's actual labels
  • Use loc[] instead when the selection is naturally based on meaningful labels rather than position
  • Remember iloc[]'s slice endpoint is exclusive, matching ordinary Python slicing, unlike loc[]'s inclusive label-based slicing

Interview Question

If a DataFrame's index has been shuffled into a non-sequential order, why would df.iloc[0] still reliably return the DataFrame's visually first row, while df.loc[0] might raise a KeyError or return an unexpected row?

Hint: Think about what each accessor actually keys off of — position versus label.

iloc[0] always means the row at physical position 0 in the DataFrame, completely independent of whatever label that row happens to carry, so it reliably returns the first row as displayed, regardless of how the index has been reordered or relabeled. loc[0] instead looks specifically for a row whose index label is the value 0 — if the index has been shuffled or replaced such that no row carries the label 0, or a different row now carries that label, loc[0] either raises a KeyError or returns a completely different row than the visually first one, since it's matching by label, not position.

Exercises

MediumPractice using df.iloc[] in a real scenario.
View Solution
import pandas as pd

df = pd.DataFrame({"name": ["Alice", "Bob", "Carol"]}, index=["x", "y", "z"])
print(df.iloc[0])

Frequently Asked Questions

If a DataFrame's index has been shuffled into a non-sequential order, why would df.iloc[0] still reliably return the DataFrame's visually first row, while df.loc[0] might raise a KeyError or return an unexpected row?

iloc[0] always means the row at physical position 0 in the DataFrame, completely independent of whatever label that row happens to carry, so it reliably returns the first row as displayed, regardless of how the index has been reordered or relabeled. loc[0] instead looks specifically for a row whose index label is the value 0 — if the index has been shuffled or replaced such that no row carries the label 0, or a different row now carries that label, loc[0] either raises a KeyError or returns a completely different row than the visually first one, since it's matching by label, not position.

Related Functions

df-locdf-iatnp-array