Often you may want to select the rows of a pandas DataFrame based on their index value.
If you’d like to select rows based on integer indexing, you can use the .iloc function.
If you’d like to select rows 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.
Example 1: Select Rows Based on Integer Indexing
The following code shows how to create a pandas DataFrame and use .iloc to select the row with an index integer value of 4:
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 the 5th row of the DataFrame df.iloc[[4]] A B 12 0.963663 0.383442
We can use similar syntax to select multiple rows:
#select the 3rd, 4th, and 5th rows of the DataFrame
df.iloc[[2, 3, 4]]
A B
6 0.423655 0.645894
9 0.437587 0.891773
12 0.963663 0.383442
Or we could select all rows in a range:
#select the 3rd, 4th, and 5th rows of the DataFrame
df.iloc[2:5]
A B
6 0.423655 0.645894
9 0.437587 0.891773
12 0.963663 0.383442
Example 2: Select Rows Based on Label Indexing
The following code shows how to create a pandas DataFrame and use .loc to select the row with an index label of 3:
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 the row with index label '3' df.loc[[3]] A B 3 0.602763 0.544883
We can use similar syntax to select multiple rows with different index labels:
#select the rows with index labels '3', '6', and '9'
df.loc[[3, 6, 9]]
A B
3 0.602763 0.544883
6 0.423655 0.645894
9 0.437587 0.891773
Example 3: Selecting Rows with Negative Indexing Using .iloc
Pandas also allows you to use negative integers with .iloc to select rows from the end of the DataFrame. For example, using -1 refers to the last row, -2 refers to the second-to-last row, and so on.
The following example demonstrates how to select rows using negative indexing:
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 the last row of the DataFrame df.iloc[[-1]] A B 15 0.791725 0.528895
We can also select multiple rows from the end:
#select the last two rows of the DataFrame
df.iloc[[-2, -1]]
A B
12 0.963663 0.383442
15 0.791725 0.528895
Or we can mix positive and negative indices:
#select the first and last rows of the DataFrame
df.iloc[[0, -1]]
A B
0 0.548814 0.715189
15 0.791725 0.528895
Example 4: Selecting Rows Using Boolean Indexing on the Index
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
We can also use more complex conditions with logical operators:
#select rows where index is between 3 and 12
df[(df.index >= 3) & (df.index <= 12)]
A B
3 0.602763 0.544883
6 0.423655 0.645894
9 0.437587 0.891773
12 0.963663 0.383442
Or use other methods like .isin() to select rows with specific index values:
#select rows where index is in a specific list of values
df[df.index.isin([0, 6, 15])]
A B
0 0.548814 0.715189
6 0.423655 0.645894
15 0.791725 0.528895
The Difference Between .iloc and .loc
The examples above illustrate the subtle difference between .iloc an .loc:
- .iloc selects rows based on an integer index. So, if you want to select the 5th row in a DataFrame, you would use df.iloc[[4]] since the first row is at index 0, the second row is at index 1, and so on.
- .loc selects rows based on a labeled index. So, if you want to select the row with an index label of 5, you would directly use df.loc[[5]].
Wrapping Up
Selecting the right rows from a pandas DataFrame is an essential skill for data analysis in Python. Here’s a quick summary of the methods we’ve covered:
- .iloc provides integer-based indexing for selecting rows by position (0-based), including with negative indices to count from the end.
- .loc provides label-based indexing for selecting rows by their index values.
- Boolean indexing on the index allows you to filter rows based on conditions applied to index values.
These methods can be combined with column selection techniques to extract specific subsets of data effectively. By understanding these selection methods, you can efficiently manipulate and analyze data in pandas.
Additional Resources
How to Get Row Numbers in a Pandas DataFrame
How to Drop Rows with NaN Values in Pandas
How to Drop the Index Column in Pandas
Thank you.