Rows in a Pandas DataFrame represent individual records or observations and accessing them efficiently is key to data manipulation.
Accessing rows in a Pandas DataFrame is fundamental for data manipulation and analysis. The most basic approach of accessing rows is using iloc function. The iloc method is used for positional indexing, allowing us to access rows by their integer position. To access a single row, use its integer index:
Python
import pandas as pd
from io import StringIO
data = """Name,Age,Gender,Salary
John,25,Male,50000
Alice,30,Female,55000
Bob,22,Male,40000
Eve,35,Female,70000
Charlie,28,Male,48000"""
df = pd.read_csv(StringIO(data))
row1 = df.iloc[0]
print(row1)
OutputName John
Age 25
Gender Male
Salary 50000
Name: 0, dtype: object
This method allows you to easily access a single column of data.
Now, let's select multiple rows using iloc with the help of slicing:
Python
# Access rows 1 to 3 (index 1 to 3)
rows = df.iloc[1:4]
print(rows)
Output Name Age Gender Salary
1 Alice 30 Female 55000
2 Bob 22 Male 40000
3 Eve 35 Female 70000
This approach enables us to select and manipulate multiple columns simultaneously.
In addition to the this method, there are several other methods to access rows in a Pandas DataFrame:
Accessing Rows Using loc
The loc method is used for label-based indexing. It allows you to access rows by their index labels (the row names). This method is useful when we know the row labels but not their integer position.
Python
# Access the row with label 2
row_label_2 = df.loc[2]
print(row_label_2)
OutputName Bob
Age 22
Gender Male
Salary 40000
Name: 2, dtype: object
Accessing Rows Using Conditions
Pandas allows us to access rows based on a condition or filter. This is useful when we want to retrieve rows that meet specific criteria.
Python
# Access rows where 'Age' is greater than 25
row_f = df[df['Age'] > 25]
print(row_f)
Output Name Age Gender Salary
1 Alice 30 Female 55000
3 Eve 35 Female 70000
4 Charlie 28 Male 48000
Accessing Specific Rows Using query
The query() method allows us to filter rows using a SQL-like syntax. This method is useful for complex queries and makes the code more readable when dealing with conditions on multiple columns.
Python
# Access rows where 'Age' is greater than 25 and 'Salary' is less than 60000
query_f = df.query('Age > 25 and Salary < 60000')
print(query_f)
Output Name Age Gender Salary
1 Alice 30 Female 55000
4 Charlie 28 Male 48000
Accessing Rows Using head and tail
The head() and tail() methods allow us to quickly access the first or last few rows of a DataFrame, respectively. These methods are useful when we want to inspect the top or bottom records in a dataset.
Python
# Access the first 2 rows
r1 = df.head(2)
print(r1)
# Access the last 2 rows
r2 = df.tail(2)
print(r2)
Output Name Age Gender Salary
0 John 25 Male 50000
1 Alice 30 Female 55000
Name Age Gender Salary
3 Eve 35 Female 70000
4 Charlie 28 Male 48000
Accessing Rows Using iloc with Conditions
We can combine iloc with conditions to access specific rows by position after applying a filter. This allows us to select rows based on criteria and then access them by their position in the filtered DataFrame.
Python
# Access the first row where 'Age' is greater than 25
row_f = df[df['Age'] > 25].iloc[0]
print(row_f)
OutputName Alice
Age 30
Gender Female
Salary 55000
Name: 1, dtype: object
For more information refer to below:
Similar Reads
Pandas Exercises and Programs Pandas is an open-source Python Library that is made mainly for working with relational or labelled data both easily and intuitively. This Python library is built on top of the NumPy library, providing various operations and data structures for manipulating numerical data and time series. Pandas is
6 min read
Pandas Access DataFrame Accessing a dataframe in pandas involves retrieving, exploring, and manipulating data stored within this structure. The most basic form of accessing a DataFrame is simply referring to it by its variable name. This will display the entire DataFrame, which includes all rows and columns.Pythonimport pa
3 min read
Pandas Slice Rows Simplest way to select rows is by using the iloc method which allows to access rows by their integer position. For instance, to slice a single row you can specify its index.Pythonimport pandas as pd data = {'Name': ['John', 'Alice', 'Bob', 'Eve'], 'Age': [25, 30, 22, 35], 'Gender': ['Male', 'Female'
2 min read
Pandas Select Columns Simplest way to select a specific or multiple columns in pandas dataframe is by using bracket notation, where you place the column name inside square brackets. Let's consider following example: Pythonimport pandas as pd data = {'Name': ['John', 'Alice', 'Bob', 'Eve', 'Charlie'], 'Age': [25, 30, 22,
3 min read
Access the elements of a Series in Pandas Pandas Series is a one-dimensional labeled array capable of holding data of any type (integer, string, float, python objects, etc.). Labels need not be unique but must be a hashable type. Let's discuss different ways to access the elements of given Pandas Series. First create a Pandas Series. Python
2 min read