Pandas DataFrame.loc


Pandas DataFrame.loc

The DataFrame.loc property in pandas is used for accessing a group of rows and columns by labels or a boolean array. This property is particularly powerful for filtering data based on conditions or selecting specific rows and columns.


Syntax

The syntax for DataFrame.loc is:

DataFrame.loc[row_labels, column_labels]

Here, row_labels and column_labels can be single labels, lists, slices, or boolean arrays to specify the rows and columns to access.


Parameters

ParameterDescription
row_labelsThe row labels or indices to select. Can be a single label, list, slice, or boolean array.
column_labelsThe column labels or names to select. Can be a single label, list, or slice. If not specified, all columns are selected.

Returns

A DataFrame, Series, or scalar value depending on the input provided.


Examples

Selecting Specific Rows

Use loc to select specific rows by their labels.

Python Program

import pandas as pd

# Create a DataFrame
data = {
    'Name': ['Arjun', 'Ram', 'Priya'],
    'Age': [25, 30, 35],
    'Salary': [70000, 80000, 90000]
}
df = pd.DataFrame(data, index=['A', 'B', 'C'])

# Select a specific row by label
print("Row 'A':")
print(df.loc['A'])

Output

Row 'A':
Name       Arjun
Age           25
Salary     70000
Name: A, dtype: object

Selecting Specific Rows and Columns

Select both specific rows and specific columns using loc.

Python Program

import pandas as pd

# Create a DataFrame
data = {
    'Name': ['Arjun', 'Ram', 'Priya'],
    'Age': [25, 30, 35],
    'Salary': [70000, 80000, 90000]
}
df = pd.DataFrame(data, index=['A', 'B', 'C'])

# Select specific rows and columns
print("Row 'B' and column 'Salary':")
print(df.loc['B', 'Salary'])

Output

Row 'B' and column 'Salary':
80000

Selecting Multiple Rows and Columns

Use lists of labels to select multiple rows and columns.

Python Program

import pandas as pd

# Create a DataFrame
data = {
    'Name': ['Arjun', 'Ram', 'Priya'],
    'Age': [25, 30, 35],
    'Salary': [70000, 80000, 90000]
}
df = pd.DataFrame(data, index=['A', 'B', 'C'])

# Select multiple rows and columns
print("Rows 'A' and 'C', and columns 'Name' and 'Salary':")
print(df.loc[['A', 'C'], ['Name', 'Salary']])

Output

Rows 'A' and 'C', and columns 'Name' and 'Salary':
    Name  Salary
A  Arjun   70000
C  Priya   90000

Using Boolean Indexing

Filter rows based on a condition using boolean indexing with loc.

Python Program

import pandas as pd

# Create a DataFrame
data = {
    'Name': ['Arjun', 'Ram', 'Priya'],
    'Age': [25, 30, 35],
    'Salary': [70000, 80000, 90000]
}
df = pd.DataFrame(data, index=['A', 'B', 'C'])

# Filter rows where Age > 25
print("Rows where Age > 25:")
print(df.loc[df['Age'] > 25])

Output

Rows where Age > 25:
    Name  Age  Salary
B    Ram   30   80000
C  Priya   35   90000

Using Slices

Select rows and columns using slices.

Python Program

import pandas as pd

# Create a DataFrame
data = {
    'Name': ['Arjun', 'Ram', 'Priya'],
    'Age': [25, 30, 35],
    'Salary': [70000, 80000, 90000]
}
df = pd.DataFrame(data, index=['A', 'B', 'C'])

# Select rows 'A' to 'B' and all columns
print("Rows 'A' to 'B':")
print(df.loc['A':'B'])

Output

Rows 'A' to 'B':
    Name  Age  Salary
A  Arjun   25   70000
B    Ram   30   80000

Summary

In this tutorial, we explored the DataFrame.loc property in pandas. Key takeaways include:

  • Using loc to access rows and columns by labels.
  • Selecting specific rows, columns, or both.
  • Filtering data using boolean indexing.
  • Using slices to access ranges of rows and columns.

The DataFrame.loc property is a versatile tool for accessing and manipulating data in pandas DataFrames.


Python Libraries