YouTip LogoYouTip

Pandas Df Iloc

Pandas df.iloc[] Function |

\n\n

Image 1: Pandas Common functions Pandas General Functions

\n\n
\n\n

iloc[] is an integer-location based indexing for selecting data in Pandas, used by row and column numbers. It differs from loc[], which is based on labels rather than positions.

\n\n

When you need to select data by position or when you don't know the exact index labels, iloc[] is the best choice. Its behavior is very similar to Python list indexing, making it intuitive for Python users.

\n\n
\n\n

Basic Syntax and Parameters

\n\n

iloc[] is an indexer for DataFrames accessed via square brackets []. It accepts integers, lists of integers, slices of integers, or boolean arrays as parameters.

\n\n

Syntax Format

\n\n
# Select single row (returns Series)\nDataFrame.iloc\n\n# Select multiple rows (returns DataFrame)\nDataFrame.iloc[[row_number1, row_number2, ...]]\n\n# Use slice to select consecutive rows\nDataFrame.iloc[start_row:end_row]\n\n# Select rows and columns\nDataFrame.iloc[row_number, column_number]\nDataFrame.iloc[row_slice, column_slice]\nDataFrame.iloc[row_list, column_list]\n
\n\n

Parameter Description

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
Parameter PositionParameter TypeDescription
First parameter (rows)integer, list of integers, slice of integers, boolean arrayUsed to select rows, based on position (starting from 0).
Second parameter (columns)integer, list of integers, slice of integersOptional, used to select columns, also based on position.
\n\n

Return Value Description

\n\n
    \n
  • Single element: Returns a scalar value.
  • \n
  • Single row: Returns a Series.
  • \n
  • Multiple rows: Returns a DataFrame.
  • \n
  • Row-column combination: Returns a Series or DataFrame depending on selection.
  • \n
\n\n
\n\n

Examples

\n\n

Let’s explore various examples to fully understand how to use iloc[].

\n\n

Example 1: Basic Usage - Selecting Rows

\n\n

iloc[] uses positional indexing, similar to Python list indexing.

\n\n

Example

\n\n
import pandas as pd\n\n# Create sample DataFrame\ndata = {\n    'name': ['Alice','Bob','Charlie','David','Eve'],\n    'age': [18,19,17,18,20],\n    'score': [85,92,78,90,88],\n    'grade': ['A','A','B','A','B']\n}\n\ndf = pd.DataFrame(data)\n\nprint("Original DataFrame:")\nprint(df)\nprint()\n\n# Select first row (index 0)\nprint("Select first row (position 0):")\nprint(df.iloc)\nprint()\n\n# Select multiple rows\nprint("Select rows 1, 3, 4:")\nprint(df.iloc[[1,3,4]])\nprint()\n\n# Use slice to select continuous rows (note: iloc slice is left-closed right-open like Python)\nprint("Select rows 1 to 3 (positions 1, 2, 3):")\nprint(df.iloc[1:4])\nprint()\n\n# Select first 3 rows\nprint("First 3 rows:")\nprint(df.iloc[:3])\n
\n\n

Output:

\n\n
Original DataFrame:\n   name  age  score grade\n0  Alice   18     85     A\n1    Bob   19     92     A\n2 Charlie   17     78     B\n3  David   18     90     A\n4    Eve   20     88     B\n\nSelect first row (position 0):\nname      Alice\nage          18\nscore        85\ngrade         A\nName: 0, dtype: object\n\nSelect rows 1, 3, 4:\n   name  age  score grade\n1    Bob   19     92     A\n3  David   18     90     A\n4    Eve   20     88     B\n\nSelect rows 1 to 3 (positions 1, 2, 3):\n   name  age  score grade\n1    Bob   19     92     A\n2 Charlie   17     78     B\n3  David   18     90     A\n\nFirst 3 rows:\n   name  age  score grade\n0  Alice   18     85     A\n1    Bob   19     92     A\n2 Charlie   17     78     B\n
\n\n

Code Explanation:

\n\n
    \n
  1. df.iloc selects the first row (position 0), returning a Series.
  2. \n
  3. iloc slice is left-closed right-open, including positions 1, 2, 3 but not 4.
  4. \n
  5. df.iloc[[1, 3, 4]] selects specific rows by their positions.
  6. \n
  7. df.iloc[:3] omits the start position, indicating selection from 0.
  8. \n
\n\n

Example 2: Selecting Specific Rows and Columns

\n\n

iloc[] can select both rows and columns using positional indices.

\n\n

Example

\n\n
import pandas as pd\n\ndata = {\n    'name': ['Alice','Bob','Charlie','David','Eve'],\n    'age': [18,19,17,18,20],\n    'score': [85,92,78,90,88],\n    'grade': ['A','A','B','A','B']\n}\n\ndf = pd.DataFrame(data)\n\n# Select single cell (second row, third column)\nprint("Select cell at position [1, 2]:")\nprint(df.iloc[1,2]) # returns 92\n\nprint()\n\n# Select specific columns of a specific row\nprint("Select column 1 and 3 of row 0:")\nprint(df.iloc[0,[1,3]])\n\nprint()\n\n# Select multiple rows and columns\nprint("Select columns 0 and 2 of rows 0, 2, 4:")\nprint(df.iloc[[0,2,4],[0,2]])\n\nprint()\n\n# Select specific columns of all rows\nprint("Columns 0 and 2 of all rows:")\nprint(df.iloc[:,[0,2]])\n\nprint()\n\n# Combine row and column slices\nprint("Rows 1 to 3, columns 0 to 2 (exclusive):")\nprint(df.iloc[1:4, :3])\n
\n\n

Output:

\n\n
Select cell at position [1, 2]:\n92\n\nSelect column 1 and 3 of row 0:\n   age grade\n0   19     A\n\nSelect columns 0 and 2 of rows 0, 2, 4:\n   name  score\n0  Alice     85\n2 Charlie     78\n4    Eve     88\n\nColumns 0 and 2 of all rows:\n   name  score\n0  Alice     85\n1    Bob     92\n2 Charlie     78\n3  David     90\n4    Eve     88\n\nRows 1 to 3, columns 0 to 2 (exclusive):\n   name  age\n1    Bob   19\n2 Charlie   17\n3  David   18\n
\n\n

Code Explanation:

\n\n
    \n
  1. df.iloc[1, 2] selects a single cell, returning a scalar value.
  2. \n
  3. df.iloc[0, [1, 3]] selects multiple columns of a specific row.
  4. \n
  5. df.iloc[:, [0, 2]] colon indicates all rows.
  6. \n
  7. iloc slicing follows Python convention: left-closed, right-open (excluding end).
  8. \n
\n\n

Example 3: Using with Custom Index

\n\n

When a DataFrame has custom index labels, iloc[] still selects based on position, regardless of the index labels.

\n\n

Example

\n\n
import pandas as pd\n\n# Create DataFrame with custom index\ndata = {\n    'name': ['Alice','Bob','Charlie','David','Eve'],\n    'age': [18,19,17,18,20],\n    'score': [85,92,78,90,88]\n}\n\ndf = pd.DataFrame(data, index=['a','b','c','d','e'])\n\nprint("DataFrame with custom index:")\nprint(df)\nprint()\n\n# iloc still selects by position, ignoring custom index\nprint("iloc selects first row (position 0):")\nprint(df.iloc)\nprint()\n\nprint("iloc[1:3] selects rows 2 and 3 (positions 1, 2):")\nprint(df.iloc[1:3])\nprint()\n\n# Use negative index (counting from end)\nprint("Last row (position -1):")\nprint(df.iloc)\nprint()\n\nprint("Last 3 rows:")\nprint(df.iloc[-3:])\n
\n\n

Output:

\n\n
DataFrame with custom index:\n       name  age  score\na     Alice   18     85\nb       Bob   19     92\nc   Charlie   17     78\nd     David   18     90\ne       Eve   20     88\n\niloc selects first row (position 0), independent of labels 'a', 'b', 'c':\n       name  age  score\na     Alice   18     85\nName: a, dtype: object\n\niloc[1:3] selects rows 2 and 3 (positions 1, 2):\n       name  age  score\nb       Bob   19     92\nc   Charlie   17     78\n\nLast row (position -1):\n       name  age  score\ne       Eve   20     88\nName: e, dtype: object\n\nLast 3 rows:\n       name  age  score\nc   Charlie   17     78\nd     David   18     90\ne       Eve   20     88\n
\n\n

Code Explanation:

\n\n
    \n
  • Even with custom index ('a', 'b', 'c', 'd', 'e'), iloc[] selects by position.
  • \n
  • iloc always selects the first row (physical position), regardless of index label.
  • \n
  • iloc supports negative indexing: -1 is last row, -2 is second-to-last, etc.
  • \n
\n\n

Example 4: Using Boolean Arrays

\n\n

iloc[] also supports selection using boolean arrays, useful for conditional filtering.

\n\n

Example

\n\n
import pandas as pd\n\ndata = {\n    'name': ['Alice','Bob','Charlie','David','Eve'],\n    'age': [18,19,17,18,20],\n    'score': [85,92,78,90,88],\n    'grade': ['A','A','B','A','B']\n}\n\ndf = pd.DataFrame(data)\n\n# Use boolean array to select\nbool_array = [True, False, True, False, True]\n\nprint("Rows selected using boolean array [True, False, True, False, True]:")\nprint(df.iloc)\nprint()\n\n# Combine with conditions (compute boolean array first, then use in iloc)\n# Select rows where score > 85\ncondition = df['score'] > 85\n\nprint("Rows where score > 85:")\nprint(df.iloc[condition.values]) # Get boolean array\n\nprint()\n\n# Select specific rows and columns\nprint("Rows 1 and 3, columns 1 and 2:")\nprint(df.iloc[[1,3],1:3])\n
\n\n

Output:

\n\n
Rows selected using boolean array [True, False, True, False, True]:\n   name  age  score grade\n0  Alice   18     85     A\n2 Charlie   17     78     B\n4    Eve   20     88     B\n\nRows where score > 85:\n   name  age  score grade\n1    Bob   19     92     A\n3  David   18     90     A\n4    Eve   20     88     B\n\nRows 1 and 3, columns 1 and 2:\n   age  score\n1   19     92\n3   18     90\n
\n\n

Code Explanation:

\n\n
    \n
  1. The length of the boolean array must match the number of rows (for row selection) or columns (for column selection).
  2. \n
  3. When using iloc with a boolean array, it directly selects based on position, unlike loc[] which filters by condition.
  4. \n
  5. You can use step slicing to select patterns, such as every other row.
  6. \n
\n\n
\n\n

Notes

\n\n
    \n
  • iloc[] uses positional indexing, and slicing follows Python conventions (left-closed, right-open).
  • \n
  • If the specified position is out of range, an IndexError will be raised.
  • \n
  • Negative indexing is supported: -1 means last row, -2 means second-to-last, etc.
  • \n
  • Different from loc[], iloc[] does not support direct conditional expressions for filtering; you must compute a boolean array first.
  • \n
\n\n
\n

Important Note: The difference between loc[] and iloc[] is a key concept in learning Pandas. loc[] is label-based, while iloc[] is position-based. When developing, clearly distinguish between them to avoid confusion and errors.

\n
\n\n
\n\n

Summary

\n\n

iloc[] is a position-based data selector in Pandas, providing an experience similar to Python list indexing. Its main feature is that it is entirely based on data position (starting from 0), unaffected by custom index labels.

\n\n

In practice, iloc[] is particularly suitable for scenarios involving:

\n\n
    \n
  • Selecting data by position
  • \n
  • Using negative indexing
  • \n
  • Using slice steps
  • \n
  • Selecting data when index labels are unknown
  • \n
\n\n

Combining loc[] and iloc[] allows flexible handling of various data selection needs.

\n\n

Image 2: Pandas Common functions Pandas General Functions

← Pandas Df FilterPandas Df Tail β†’