Get a specific row in a given Pandas DataFrame
Last Updated :
27 Nov, 2024
In the Pandas Dataframe, we can find the specified row value with the function iloc(). In this function, we pass the row number as a parameter. The core idea behind this is simple: you access the rows by using their index or position. In this article, we'll explore different ways to get a row from a Pandas DataFrame and highlight which method works best in different situations. Below is a quick example to help you understand the concept right away:
Method 1. Using iloc
for Integer-Location-Based Indexing
The iloc
method is used for integer-location-based indexing, which means you select rows and columns based on their integer positions. This is significant because it allows you to access rows and columns by their numerical indices, starting from 0.
Python
import pandas as pd
data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35], 'City': ['NY', 'LA', 'SF']}
df = pd.DataFrame(data)
# Select the second row
specific_row = df.iloc[1]
print(specific_row)
OutputName Bob
Age 30
City LA
Name: 1, dtype: object
This example shows how to extract the second row using the .iloc[]
method. Now, let's dive deeper into various methods for selecting rows. .iloc[]
is highly versatile for positional indexing, especially when working with large datasets where row labels are unknown or irrelevant.
Method 2. Using loc
for Label-Based Indexing
The loc
method is used for label-based indexing, which means you select rows and columns based on their labels. This is particularly useful when your DataFrame has custom index labels. .loc[]
allows both label-based indexing and conditional filtering, making it more intuitive for labeled datasets.
Python
import pandas as pd
data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35], 'City': ['NY', 'LA', 'SF']}
df = pd.DataFrame(data)
# Select rows where Age is greater than 25
filtered_rows = df.loc[df['Age'] > 25]
print(filtered_rows)
Output Name Age City
1 Bob 30 LA
2 Charlie 35 SF
Method 3. Using Slicing for Specific Range of rows
You can use slicing to select specific ranges of rows. This method is straightforward but limited to numerical indices. It’s simple and effective for extracting contiguous rows quickly without additional syntax.
Python
import pandas as pd
data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35], 'City': ['NY', 'LA', 'SF']}
df = pd.DataFrame(data)
# Select the first two rows
rows = df[:2]
print(rows)
Output Name Age City
0 Alice 25 NY
1 Bob 30 LA
Method 4. Combining .iloc[]
with Column Selection
You can combine .iloc[]
with column selection to extract specific cells or subsets of data. This method provides fine-grained control over both rows and columns, making it ideal for targeted data extraction.
Python
import pandas as pd
data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35], 'City': ['NY', 'LA', 'SF']}
df = pd.DataFrame(data)
# Select the 'Name' and 'City' columns for the second row
subset = df[['Name', 'City']].iloc[1]
print(subset)
OutputName Bob
City LA
Name: 1, dtype: object
Method 5. Using Boolean Indexing
Boolean indexing allows to filter rows based on conditions applied to one or more columns. Instead of manually selecting rows by index numbers, you can use logical conditions (such as greater than, less than, or equal to) to automatically identify and select the rows that meet those criteria.
Python
import pandas as pd
data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35], 'City': ['NY', 'LA', 'SF']}
df = pd.DataFrame(data)
# Select rows where City is 'NY'
ny_rows = df[df['City'] == 'NY']
print(ny_rows)
Output Name Age City
0 Alice 25 NY
In this example, df['City'] == 'New York'
creates a Boolean Series where each entry is either True
or False
based on whether the condition is met. By passing this Boolean Series into df[]
, Pandas filters the rows that correspond to True
values, effectively returning all rows where the 'City' column equals 'New York'.