Pandas df.iloc[] Function |
\n\n\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.
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
Basic Syntax and Parameters
\n\niloc[] is an indexer for DataFrames accessed via square brackets []. It accepts integers, lists of integers, slices of integers, or boolean arrays as parameters.
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\nParameter Description
\n\n| Parameter Position | \nParameter Type | \nDescription | \n
|---|---|---|
| First parameter (rows) | \ninteger, list of integers, slice of integers, boolean array | \nUsed to select rows, based on position (starting from 0). | \n
| Second parameter (columns) | \ninteger, list of integers, slice of integers | \nOptional, used to select columns, also based on position. | \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
Examples
\n\nLetβs explore various examples to fully understand how to use iloc[].
Example 1: Basic Usage - Selecting Rows
\n\niloc[] uses positional indexing, similar to Python list indexing.
Example
\n\nimport 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\nOutput:
\n\nOriginal 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\nCode Explanation:
\n\n- \n
df.ilocselects the first row (position 0), returning a Series. \nilocslice is left-closed right-open, including positions 1, 2, 3 but not 4. \ndf.iloc[[1, 3, 4]]selects specific rows by their positions. \ndf.iloc[:3]omits the start position, indicating selection from 0. \n
Example 2: Selecting Specific Rows and Columns
\n\niloc[] can select both rows and columns using positional indices.
Example
\n\nimport 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\nOutput:
\n\nSelect 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\nCode Explanation:
\n\n- \n
df.iloc[1, 2]selects a single cell, returning a scalar value. \ndf.iloc[0, [1, 3]]selects multiple columns of a specific row. \ndf.iloc[:, [0, 2]]colon indicates all rows. \nilocslicing follows Python convention: left-closed, right-open (excluding end). \n
Example 3: Using with Custom Index
\n\nWhen a DataFrame has custom index labels, iloc[] still selects based on position, regardless of the index labels.
Example
\n\nimport 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\nOutput:
\n\nDataFrame 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\nCode Explanation:
\n\n- \n
- Even with custom index ('a', 'b', 'c', 'd', 'e'),
iloc[]selects by position. \n ilocalways selects the first row (physical position), regardless of index label. \nilocsupports negative indexing: -1 is last row, -2 is second-to-last, etc. \n
Example 4: Using Boolean Arrays
\n\niloc[] also supports selection using boolean arrays, useful for conditional filtering.
Example
\n\nimport 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\nOutput:
\n\nRows 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\nCode Explanation:
\n\n- \n
- The length of the boolean array must match the number of rows (for row selection) or columns (for column selection). \n
- When using
ilocwith a boolean array, it directly selects based on position, unlikeloc[]which filters by condition. \n - You can use step slicing to select patterns, such as every other row. \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
IndexErrorwill 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\nImportant Note: The difference between
\nloc[]andiloc[]is a key concept in learning Pandas.loc[]is label-based, whileiloc[]is position-based. When developing, clearly distinguish between them to avoid confusion and errors.
\n\n
Summary
\n\niloc[] 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.
In practice, iloc[] is particularly suitable for scenarios involving:
- \n
- Selecting data by position \n
- Using negative indexing \n
- Using slice steps \n
- Selecting data when index labels are unknown \n
Combining loc[] and iloc[] allows flexible handling of various data selection needs.
YouTip