echoing @HYRY, see the new docs in 0.11

http://pandas.pydata.org/pandas-docs/stable/indexing.html

Here we have new operators, .iloc to explicity support only integer indexing, and .loc to explicity support only label indexing

e.g. imagine this scenario

In [1]: df = pd.DataFrame(np.random.rand(5,2),index=range(0,10,2),columns=list('AB'))

In [2]: df
Out[2]: 
          A         B
0  1.068932 -0.794307
2 -0.470056  1.192211
4 -0.284561  0.756029
6  1.037563 -0.267820
8 -0.538478 -0.800654

In [5]: df.iloc[[2]]
Out[5]: 
          A         B
4 -0.284561  0.756029

In [6]: df.loc[[2]]
Out[6]: 
          A         B
2 -0.470056  1.192211

[] slices the rows (by label location) only

Answer from Jeff on Stack Overflow
Top answer
1 of 8
815

echoing @HYRY, see the new docs in 0.11

http://pandas.pydata.org/pandas-docs/stable/indexing.html

Here we have new operators, .iloc to explicity support only integer indexing, and .loc to explicity support only label indexing

e.g. imagine this scenario

In [1]: df = pd.DataFrame(np.random.rand(5,2),index=range(0,10,2),columns=list('AB'))

In [2]: df
Out[2]: 
          A         B
0  1.068932 -0.794307
2 -0.470056  1.192211
4 -0.284561  0.756029
6  1.037563 -0.267820
8 -0.538478 -0.800654

In [5]: df.iloc[[2]]
Out[5]: 
          A         B
4 -0.284561  0.756029

In [6]: df.loc[[2]]
Out[6]: 
          A         B
2 -0.470056  1.192211

[] slices the rows (by label location) only

2 of 8
124

The primary purpose of the DataFrame indexing operator, [] is to select columns.

When the indexing operator is passed a string or integer, it attempts to find a column with that particular name and return it as a Series.

So, in the question above: df[2] searches for a column name matching the integer value 2. This column does not exist and a KeyError is raised.


The DataFrame indexing operator completely changes behavior to select rows when slice notation is used

Strangely, when given a slice, the DataFrame indexing operator selects rows and can do so by integer location or by index label.

df[2:3]

This will slice beginning from the row with integer location 2 up to 3, exclusive of the last element. So, just a single row. The following selects rows beginning at integer location 6 up to but not including 20 by every third row.

df[6:20:3]

You can also use slices consisting of string labels if your DataFrame index has strings in it. For more details, see this solution on .iloc vs .loc.

I almost never use this slice notation with the indexing operator as its not explicit and hardly ever used. When slicing by rows, stick with .loc/.iloc.

๐ŸŒ
Pandas
pandas.pydata.org โ€บ pandas-docs โ€บ stable โ€บ user_guide โ€บ indexing.html
Indexing and selecting data โ€” pandas 3.0.5 documentation
Trying to use a non-integer, even a valid label will raise an IndexError. The .iloc attribute is the primary access method. The following are valid inputs: An integer e.g. 5. A list or array of integers [4, 3, 0]. A slice object with ints 1:7. A boolean array. A callable, see Selection By Callable. A tuple of row (and column) indexes, whose elements are one of the above types.
Discussions

python - Select Pandas rows based on list index - Stack Overflow
If callable, the callable function ... if the row should be skipped and False otherwise. An example of a valid callable argument would be lambda x: x in [0, 2] This feature works in version pandas 0.20.0+. See also the corresponding issue and a related post. ... Save this answer. ... Show activity on this post. What you are trying to do is to filter your dataframe by index... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to extract rows from a Pandas dataframe using .loc[]? - Python - Data Science Dojo Discussions
The Pandas loc[] method is used to select rows from a Pandas dataframe based on their index labels. It takes a list of index labels as input and returns a new dataframe containing only the rows with those labels. This method is useful for extracting specific rows from a dataframe or for subsetting ... More on discuss.datasciencedojo.com
๐ŸŒ discuss.datasciencedojo.com
1
0
November 24, 2022
How to select a row by index and replace a value.
return it back to the original df It would be a lot easier to just make a brand new df, and then replace the old one with the new one. keys = df['Item'].unique() values = ["_".join(df['Model #s'][df["Item"]==key]) for key in keys] new_df = pd.DataFrame(zip(keys, values), columns=['Item','Model #s']) df = new_df # replace the old df with the new one More on reddit.com
๐ŸŒ r/learnpython
3
1
December 27, 2023
How to group rows in a dataframe by consecutive values in sequence
df[โ€œeventโ€] = ((df.frame - df.frame.shift() - 1) != 0).cumsum() More on reddit.com
๐ŸŒ r/learnpython
7
1
March 12, 2022
๐ŸŒ
Statology
statology.org โ€บ home โ€บ how to select rows by index in a pandas dataframe
How to Select Rows by Index in a Pandas DataFrame
March 27, 2025 - Another useful way to select rows is by using boolean conditions on the index. This method gives you flexibility to filter rows based on specific conditions applied to the index values. Letโ€™s look at how to select rows where the index is greater than a certain value: import pandas as pd import numpy as np #make this example reproducible np.random.seed(0) #create DataFrame df = pd.DataFrame(np.random.rand(6,2), index=range(0,18,3), columns=['A', 'B']) #view DataFrame df A B 0 0.548814 0.715189 3 0.602763 0.544883 6 0.423655 0.645894 9 0.437587 0.891773 12 0.963663 0.383442 15 0.791725 0.528895 #select rows where index is greater than 6 df[df.index > 6] A B 9 0.437587 0.891773 12 0.963663 0.383442 15 0.791725 0.528895
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python โ€บ pandas
pandas: Select rows/columns by index (numbers and names) | note.nkmk.me
August 8, 2023 - s_bool_wrong = pd.Series([True, False, False, True, True], index=['A', 'B', 'C', 'D', 'E']) # print(df[s_bool_wrong]) # IndexingError: Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match). ... Using a Boolean Series, you can select rows by conditions.
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ pandas โ€บ pandas select rows by index (position/label)
Pandas Select Rows by Index (Position/Label) - Spark By {Examples}
November 14, 2024 - Use Pandas DataFrame.iloc[] & DataFrame.loc[] to select rows by integer Index and by row indices respectively. iloc[] attribute can accept single
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ pandas โ€บ pandas select rows based on list index
Pandas Select Rows Based on List Index - Spark By {Examples}
October 28, 2024 - You can select rows in a Pandas DataFrame based on a list of indices, you can use the DataFrame.iloc[], DataFrame.loc] methods. iloc[] takes row indexes
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ pandas โ€บ select-rows-columns-by-name-or-index-in-pandas-dataframe-using-loc-iloc
How to Select Rows & Columns by Name or Index in Pandas Dataframe - Using loc and iloc - GeeksforGeeks
November 28, 2024 - The .iloc[] method selects data based on integer positions (index numbers). It is particularly useful when you donโ€™t know the labels but know the positions. ... Uses integer positions (0, 1, 2, ...) to index rows and columns.
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ pandas โ€บ indexing-and-selecting-data-with-pandas
Indexing and Selecting Data with Pandas - GeeksforGeeks
The .iloc[] function is used for position-based indexing. It allows us to access rows and columns by their integer positions. It is similar to .loc[] but only accepts integer-based indices to specify rows and columns.
Published: April 28, 2026
๐ŸŒ
Codegive
codegive.com โ€บ blog โ€บ pandas_choose_row_by_index.php
Mastering Pandas Choose Row by Index: Unlock Precision Data Selection & Supercharge Your Analysis!
To select a row by index in pandas, use .loc[] for label-based indexing (where the index values themselves are labels) or .iloc[] for integer-position based indexing (where you select by the row's numerical position).
๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ pandas-select-rows-by-index-list
Pandas: Select rows based on a List of Indices | bobbyhadz
April 12, 2024 - Use the DataFrame.iloc position-based indexer to select rows in a DataFrame based on a list of indices. The iloc property will return a new DataFrame containing only the rows at the specified indices.
๐ŸŒ
Medium
medium.com โ€บ @amit25173 โ€บ how-to-use-pandas-get-row-by-index-b01fa9339cdf
How to Use pandas Get Row by Index? | by Amit Yadav | Medium
April 12, 2025 - ... To get a specific row by its index, you can use the .loc or .iloc methods. The main difference is that .loc accesses rows based on label-based indexing, while .iloc is entirely integer-location-based.
๐ŸŒ
Python Examples
pythonexamples.org โ€บ pandas-dataframe-select-rows-based-on-index-condition
Pandas DataFrame - Select rows based on Index condition
We shall use the condition that the index should be exactly divisible by 2. Follow the same steps as in the previous example, except for the condition in Step 3. The condition to select even indexed rows is as shown below. ... The complete program to select rows from a DataFrame whose index value is an even number is even. import pandas as pd # Take a DataFrame df = pd.DataFrame({ 'name': ['apple', 'banana', 'cherry', 'fig', 'mango'], 'quantity': [14, 0, 0, 37, 25], 'price': [100, 50, 20, 30, 150] }) # Select rows whose index is even df_selected_rows = df[df.index % 2 == 0] # Print DataFrame print(f"Original DataFrame\n{df}\n") print(f"Selected Rows\n{df_selected_rows}")
๐ŸŒ
YouTube
youtube.com โ€บ watch
Select Rows of pandas DataFrame by Index in Python (2 Examples) | Extract & Get Row | Multiple Lines - YouTube
How to extract pandas DataFrame rows by index positions in the Python programming language. More details: https://statisticsglobe.com/select-rows-of-pandas-d...
Published: January 18, 2023
๐ŸŒ
Machine Learning Plus
machinelearningplus.com โ€บ blog โ€บ pandas iloc โ€“ how to select rows using index in dataframes?
Pandas iloc - How to select rows using index in DataFrames? - machinelearningplus
March 8, 2022 - You can pass a single integer value as the row index to select a single row across all the columns from the dataframe. ... By specifying both the row and column indices to the iloc function, you can also view a specific data point.
๐ŸŒ
Saturn Cloud
saturncloud.io โ€บ blog โ€บ how-to-search-pandas-data-frame-by-index-value-and-value-in-any-column
How to Search Pandas Data Frame by Index Value and Value in Any Column | Saturn Cloud Blog
May 1, 2026 - To search a pandas data frame by index value, you can use the .loc[] method. The .loc[] method allows you to select rows and columns by label, and it can accept a variety of input formats.
๐ŸŒ
Statology
statology.org โ€บ home โ€บ how to select columns by index in a pandas dataframe
How to Select Columns by Index in a Pandas DataFrame
March 27, 2025 - If youโ€™d like to select columns based on label indexing, you can use the .loc function. This tutorial provides an example of how to use each of these functions in practice. The following code shows how to create a pandas DataFrame and use .iloc to select the column with an index integer value of 3:
๐ŸŒ
Shane Lynn
shanelynn.ie โ€บ home โ€บ pandas iloc and loc โ€“ quickly select rows and columns in dataframes
Pandas iloc and loc โ€“ quickly select data in DataFrames
October 16, 2021 - The iloc indexer syntax is data.iloc[<row selection>, <column selection>], which is sure to be a source of confusion for R users. โ€œilocโ€ in pandas is used to select rows and columns by number, in the order that they appear in the data frame.
๐ŸŒ
IncludeHelp
includehelp.com โ€บ python โ€บ select-pandas-rows-based-on-list-index.aspx
Select Pandas rows based on list index
When it comes to selecting a row or column value, we always use pandas.DataFrame.loc property or pandas.DataFrame.iloc property. To select pandas rows based on the list index, we will select an index of those rows with certain sequence numbers which indicate a list. If we want row 1 and row ...
๐ŸŒ
Data Science Dojo
discuss.datasciencedojo.com โ€บ python
How to extract rows from a Pandas dataframe using .loc[]? - Python - Data Science Dojo Discussions
November 24, 2022 - The Pandas loc[] method is used to select rows from a Pandas dataframe based on their index labels. It takes a list of index labels as input and returns a new dataframe containing only the rows with those labels.