Pandas

Pandas Access Row by index

Pandas: How to Access Row by Index

In Pandas, you can access rows in a DataFrame by their index. This is useful when you want to retrieve specific rows based on their position or label. There are several ways to access rows by index, including using loc[], iloc[], and direct indexing. In this article, we will cover these methods and provide examples.

Method 1: Access Row by Index Using loc[]

loc[] is used to access rows by their label (index name). If your DataFrame has a labeled index (e.g., strings or custom labels), you can use loc[] to access rows by the label.

Example: Access Row by Label Using loc[]

import pandas as pd

# Sample DataFrame with labeled index
data = {'Name': ['John', 'Alice', 'Bob'],
        'Age': [25, 30, 22],
        'Gender': ['Male', 'Female', 'Male']}
df = pd.DataFrame(data, index=['a', 'b', 'c'])

# Access row by index label 'b'
row = df.loc['b']
print(row)

Output:

Name      Alice
Age          30
Gender    Female
Name: b, dtype: object

In this example, we use loc[] to access the row with index label ‘b’. The result is the row where the ‘Name’ is ‘Alice’, ‘Age’ is 30, and ‘Gender’ is ‘Female’.

Method 2: Access Row by Index Using iloc[]

iloc[] is used to access rows by their integer position (index position) rather than the index label. This is useful when you need to access rows by their position, regardless of the index label.

Example: Access Row by Index Position Using iloc[]

# Access row at position 1 (second row, index 'b')
row = df.iloc[1]
print(row)

Output:

Name      Alice
Age          30
Gender    Female
Name: b, dtype: object

In this example, we use iloc[] to access the second row (position 1). Since the second row corresponds to the index label ‘b’, the output is the same as the previous example.

Method 3: Access Row by Index Using Direct Indexing

If you want to access a specific row based on its position, you can use direct indexing on the DataFrame. This method works like a list index in Python and returns a row as a Series.

Example: Access Row by Index Using Direct Indexing

# Access row at position 0 (first row, index 'a')
row = df[0:1]
print(row)

Output:

   Name  Age Gender
a  John   25   Male

In this example, we use direct indexing (i.e., df[0:1]) to access the first row of the DataFrame. The result is a DataFrame with the row corresponding to index ‘a’. Note that the output is still a DataFrame with one row.

Method 4: Access Multiple Rows by Index

If you want to access multiple rows, you can use a list of labels with loc[] or a list of index positions with iloc[].

Example: Access Multiple Rows Using loc[]

# Access rows with index labels 'a' and 'c'
rows = df.loc[['a', 'c']]
print(rows)

Output:

   Name  Age Gender
a  John   25   Male
c   Bob   22   Male

Example: Access Multiple Rows Using iloc[]

# Access rows at positions 0 and 2 (first and third rows)
rows = df.iloc[[0, 2]]
print(rows)

Output:

   Name  Age Gender
a  John   25   Male
c   Bob   22   Male

Summary

In Pandas, you can access rows by their index using several methods such as loc[], iloc[], and direct indexing. loc[] is used for label-based indexing, while iloc[] is for integer position-based indexing. Direct indexing allows you to access rows by their position, and you can also access multiple rows by passing a list of labels or positions.

admin

Share
Published by
admin

Recent Posts

Pandas Access Column by Name

Pandas: How to Access Columns by Name In Pandas, accessing columns by name is a…

1 month ago

Pandas Accessing Columns by index

Pandas: How to Access or Select Columns by Index, not by Name In Pandas, accessing…

1 month ago

Pandas Access column using iterrows

Pandas: How to Access a Column Using iterrows() In Pandas, iterrows() is commonly used to…

1 month ago

Pandas Update Values in iterrows

Pandas - How to Update Values in iterrows In Pandas, iterrows() is a popular method…

1 month ago

Pandas iterrows keyerror – How to Fix

Pandas KeyError When Using iterrows() In Pandas, the iterrows() method is often used to iterate…

1 month ago

Pandas keyerror: 0 – How to Fix

Pandas DataFrame KeyError: 0 - Trying to access column or index that does not exist…

1 month ago