Extracting rows using Pandas .iloc[]



Pandas is a powerful library in Python used for data manipulation and analysis. It provides various methods for filtering and selecting data. In this article, we will focus on extracting rows using the Pandas iloc[] method.

What is iloc[]?

The iloc[] method is used to extract rows based on their index. The index can be either a single integer or a range of integers. It is a purely integer-based indexing system.

iloc[] Syntax

The basic syntax of iloc[] is:

dataframe.iloc[row_index, column_index]

Extracting a Single Row using iloc[]

To extract a single row, we pass the row index as an integer:

import pandas as pd

data = {'Name': ['John', 'Emma', 'Peter', 'Samantha'],
        'Age': [25, 18, 30, 21],
        'Country': ['USA', 'Canada', 'UK', 'Australia']}

df = pd.DataFrame(data)

# Extract the second row
row = df.iloc[1]
print(row)

Extracting Multiple Rows using iloc[]

To extract multiple rows, we pass a range of row indices:

# Extract the second and third rows
rows = df.iloc[1:3]
print(rows)

Extracting Rows and Columns using iloc[]

We can also extract rows and columns simultaneously:

# Extract the second and third rows and first and third columns
data = df.iloc[1:3, [0, 2]]
print(data)

Example #1 :

We are creating a DataFrame with random data and use the .iloc[] method to select all rows where age is greater than or equal to 30 and salary is greater than 60000. Below code will print the resulting filtered DataFrame.

import pandas as pd

#create a DataFrame with random data

data = {'name': ['John', 'Mary', 'Mike', 'Lisa', 'David'],
'age': [25, 31, 45, 19, 36],
'gender': ['M', 'F', 'M', 'F', 'M'],
'salary': [50000, 60000, 70000, 45000, 80000],
'dept': ['Sales', 'Marketing', 'Sales', 'HR', 'IT']}

df = pd.DataFrame(data)
#select rows using .iloc[]

# select all rows where age is greater than
# or equal to 30 and salary is greater than 60000

df_filtered = df.iloc[(df['age'] >= 30) & (df['salary'] > 60000)]

print(df_filtered)

Example #2 :

Using .iloc[] is to select a specific range of rows based on their index position

import pandas as pd

# create a DataFrame with random data

data = {'name': ['John', 'Mary', 'Mike', 'Lisa', 'David'],
'age': [25, 31, 45, 19, 36],
'gender': ['M', 'F', 'M', 'F', 'M'],
'salary': [50000, 60000, 70000, 45000, 80000],
'dept': ['Sales', 'Marketing', 'Sales', 'HR', 'IT']}

df = pd.DataFrame(data)

#select rows using .iloc[]
# select rows with index positions 1 to 3 (inclusive)
df_filtered = df.iloc[1:4]

print(df_filtered)

Conclusion

Pandas iloc[] method is an efficient way of extracting rows based on their index. It provides various functionalities to extract rows and columns simultaneously.

Reference –

Last Updated on May 13, 2023 by admin

Leave a Reply

Your email address will not be published. Required fields are marked *

Recommended Blogs