Pandas loc[] in pytho

Share this article

In Python’s Pandas library, loc[] is used to access rows and columns in a DataFrame using labels (names) or boolean conditions. It provides a way to select data by explicitly specifying the row and column labels, making it easy to filter, modify, or view specific parts of the data.

Example

import pandas as pd

data = {
    'Name': ['Alice', 'Bob', 'Charlie'],
    'Age': [25, 30, 35],
    'City': ['New York', 'Los Angeles', 'Chicago']
}

df = pd.DataFrame(data)

# Using loc to select a row by label
print(df.loc[0])  # Selects the row with index label 0

# Using loc to select specific rows and columns
print(df.loc[0:1, ['Name', 'Age']])  # Selects rows with labels 0 and 1, and columns 'Name' and 'Age'

What it does

loc[] is designed to select data using labels. You specify the row labels first and then the column labels. When using slices (0:1), both the start and end labels are included, unlike Python’s usual slicing behavior.

  • Access rows: Use df.loc[row_label] to select rows by their index label.
  • Access rows and columns: Use df.loc[row_label, column_label] to select specific rows and columns.
  • Boolean indexing: Use conditions within loc[] to filter rows based on their values.

Examples

Example 1: Select a specific row by label

print(df.loc[1])

This selects and displays the row with the index label 1. The output will be:

Name       Bob
Age         30
City    Los Angeles
Name: 1, dtype: object

Example 2: Select specific rows and columns

print(df.loc[0:2, ['Name', 'City']])

This selects rows with labels 0 to 2 and only the columns ‘Name’ and ‘City’. The output looks like this:

     Name         City
0   Alice     New York
1     Bob  Los Angeles
2  Charlie     Chicago

Example 3: Boolean indexing with loc[]

print(df.loc[df['Age'] > 25])

This uses a condition to filter rows where the ‘Age’ column is greater than 25. The output will be:

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

Example 4: Modify data using loc[]

df.loc[0, 'Age'] = 26
print(df)

This updates the value in the ‘Age’ column of the row with index 0 to 26. The DataFrame now looks like this:

      Name  Age         City
0    Alice   26     New York
1      Bob   30  Los Angeles
2  Charlie   35      Chicago