Contents
Pandas DataFrame – Select Rows based on Index Condition
In Pandas DataFrame, you can select rows by applying a condition on the index using boolean indexing.
In this tutorial, we shall go through examples where we shall select rows from a DataFrame, based on index condition, where the condition is applied on the index of DataFrame.
1. Select rows from DataFrame where index is greater than 2
In this example, we are given a DataFrame in df, and we need to select rows whose index is greater than 2, using boolean indexing.
Steps
- Import pandas library.
import pandas as pd
- Given DataFrame in df.
df = pd.DataFrame({
'name': ['apple', 'banana', 'cherry', 'fig', 'mango'],
'quantity': [14, 0, 0, 37, 25],
'price': [100, 50, 20, 30, 150]
})
- Use boolean indexing and select rows where the index is greater than 2.
df[df.index > 2]
The expression df.index > 2 creates a boolean mask where each row is marked as True if the index of the row is greater than 2, or False otherwise. Then, this mask is used to index the DataFrame, selecting only the rows where the condition is True.
In total, the expression returns a new DataFrame with the selected rows that satisfy the given condition based on the index. Assign it to a variable, say df_selected_rows.
- You may print the resulting DataFrame df_selected_rows to output.
Program
The complete program to select rows from a DataFrame where the index is greater than 2, is given below.
Python Program
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 by condition on index
df_selected_rows = df[df.index > 2]
# Print DataFrame
print(f"Original DataFrame\n{df}\n")
print(f"Selected Rows\n{df_selected_rows}")
Run Code CopyOutput
Original DataFrame
name quantity price
0 apple 14 100
1 banana 0 50
2 cherry 0 20
3 fig 37 30
4 mango 25 150
Selected Rows
name quantity price
3 fig 37 30
4 mango 25 150
2. Select even indexed rows from DataFrame
In this example, we shall select rows from DataFrame whose index is an even number. We shall use the condition that the index should be exactly divisible by 2.
Steps
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.
df[df.index % 2 == 0]
Program
The complete program to select rows from a DataFrame whose index value is an even number is even.
Python Program
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}")
Run Code CopyOutput
Original DataFrame
name quantity price
0 apple 14 100
1 banana 0 50
2 cherry 0 20
3 fig 37 30
4 mango 25 150
Selected Rows
name quantity price
0 apple 14 100
2 cherry 0 20
4 mango 25 150
Only even indexed rows are selected in the resulting DataFrame.
Summary
In this Pandas Tutorial, we learned how to select rows from a DataFrame based on index condition. We have seen examples where we selected rows when the index is greater than 2, or index is even. Similarly, you can apply a condition on the index as per your requirement, and select the rows from DataFrame accordingly.