WikiGalaxy

Personalize

Pandas loc Indexing

Introduction to loc

The loc method in Pandas is used for label-based indexing. This means you can access rows and columns by their labels, which are typically the row and column names.


import pandas as pd

# Creating a DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie'],
        'Age': [25, 30, 35],
        'City': ['New York', 'Los Angeles', 'Chicago']}
df = pd.DataFrame(data)

# Using loc to access a row by label
result = df.loc[0]
print(result)
        

Label-Based Access

In this example, we access the first row of the DataFrame using its label, which is the index value '0'. The loc method allows for intuitive access using row names.

Console Output:

Name Alice Age 25 City New York Name: 0, dtype: object

Accessing Multiple Rows and Columns

You can also use loc to access multiple rows and specific columns by passing lists of labels.


# Accessing multiple rows and specific columns
result = df.loc[[0, 2], ['Name', 'City']]
print(result)
        

Multiple Label Access

This code snippet shows how to access the first and third rows while selecting only the 'Name' and 'City' columns. The loc method provides flexibility for accessing specific parts of the DataFrame.

Console Output:

Name City 0 Alice New York 2 Charlie Chicago

Conditional Selection

The loc method can be combined with conditions to filter data. This is useful for selecting rows based on specific criteria.


# Selecting rows where Age is greater than 28
result = df.loc[df['Age'] > 28]
print(result)
        

Filter with Conditions

Here, we use loc to filter the DataFrame for rows where the 'Age' column's value is greater than 28. This demonstrates loc's ability to handle complex queries.

Console Output:

Name Age City 1 Bob 30 Los Angeles 2 Charlie 35 Chicago

Updating Values

With loc, you can also update values in the DataFrame. This is done by specifying the row and column labels and assigning new values.


# Updating a value in the DataFrame
df.loc[1, 'City'] = 'San Francisco'
print(df)
        

Modify Data

In this example, we update the 'City' for the second row to 'San Francisco'. The loc method is powerful for modifying data directly in the DataFrame.

Console Output:

Name Age City 0 Alice 25 New York 1 Bob 30 San Francisco 2 Charlie 35 Chicago

Accessing a Single Value

The loc method can be used to access a single value by specifying both the row and column labels.


# Accessing a single value
value = df.loc[2, 'Age']
print(value)
        

Single Value Access

This example demonstrates how to retrieve a single value using loc, which is the 'Age' of the third row. This is useful for retrieving specific data points.

Console Output:

35

Pandas iloc Indexing

Introduction to iloc

The iloc method in Pandas is used for integer-based indexing. This allows you to select rows and columns by their integer positions.


import pandas as pd

# Creating a DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie'],
        'Age': [25, 30, 35],
        'City': ['New York', 'Los Angeles', 'Chicago']}
df = pd.DataFrame(data)

# Using iloc to access a row by position
result = df.iloc[0]
print(result)
        

Position-Based Access

In this example, we access the first row of the DataFrame using its integer position '0'. The iloc method provides straightforward access using row indices.

Console Output:

Name Alice Age 25 City New York Name: 0, dtype: object

Accessing Multiple Rows and Columns

You can also use iloc to access multiple rows and specific columns by specifying integer index ranges.


# Accessing multiple rows and specific columns
result = df.iloc[[0, 2], [0, 2]]
print(result)
        

Multiple Index Access

This code shows how to access the first and third rows while selecting only the first and third columns. The iloc method allows for flexible data selection using indices.

Console Output:

Name City 0 Alice New York 2 Charlie Chicago

Slicing Data

With iloc, you can easily slice data by specifying start and end indices for rows and columns.


# Slicing rows and columns
result = df.iloc[0:2, 0:2]
print(result)
        

Data Slicing

This example demonstrates slicing the first two rows and the first two columns. The iloc method is efficient for extracting sub-sections of the DataFrame.

Console Output:

Name Age 0 Alice 25 1 Bob 30

Updating Values

With iloc, you can update values in the DataFrame by specifying the integer position of the elements.


# Updating a value in the DataFrame
df.iloc[1, 2] = 'San Francisco'
print(df)
        

Modify Data by Position

In this example, we update the 'City' for the second row to 'San Francisco'. The iloc method is useful for direct data manipulation using indices.

Console Output:

Name Age City 0 Alice 25 New York 1 Bob 30 San Francisco 2 Charlie 35 Chicago

Accessing a Single Value

The iloc method can be used to access a single value by specifying both the row and column indices.


# Accessing a single value
value = df.iloc[2, 1]
print(value)
        

Single Value Access by Position

This example demonstrates how to retrieve a single value using iloc, which is the 'Age' of the third row. This is useful for retrieving specific data points by position.

Console Output:

35

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025