Skip to main content

Pandas query()

In this tutorial, you will learn about the query() method in Pandas with the help of examples.

The query() method in Pandas is used to extract rows from a DataFrame that satisfy specific conditions.

Example

import pandas as pd

# sample DataFrame
data = {'A': [1, 2, 3],
        'B': [4, 5, 6]}

df = pd.DataFrame(data)

# query the DataFrame where column 'A' is less than 3 result = df.query('A < 3')
print(result) ''' Output A B 0 1 4 1 2 5 '''

query() Syntax

The syntax of the query() method in Pandas is:

df.query(expr, inplace=False, **kwargs)

query() Arguments

The query() method has the following arguments:

  • expr: the query string to evaluate
  • inplace (optional): whether the query should modify the data in place or return a modified copy
  • **kwargs: additional keyword arguments to pass to the query.

query() Return Value

The query() method returns a DataFrame resulting from the query expression.


Example 1: Basic Query

import pandas as pd

data = {'A': [1, 3, 5, 7],
        'B': [2, 4, 6, 8]}
df = pd.DataFrame(data)

# query the DataFrame for rows where 'A' is greater than 4
result = df.query('A > 4')

print(result)

Output

   A  B
2  5  6
3  7  8

In this example, we returned the rows with A > 4.


Example 2: Query Using Index

import pandas as pd

data = {'A': [1, 3, 5, 7],
        'B': [2, 4, 6, 8]}
df = pd.DataFrame(data)

# query the DataFrame using index
result = df.query('index > 1')

print(result)

Output

   A  B
2  5  6
3  7  8

In this example, we returned the rows with index > 1.


Example 3: Query with a Variable

import pandas as pd

data = {'A': [1, 3, 5, 7],
        'B': [2, 4, 6, 8]}
df = pd.DataFrame(data)

# set a threshold
threshold = 4

# query the DataFrame with a variable
result = df.query('A > @threshold')

print(result)

Output

   A  B
2  5  6
3  7  8

In this example, we returned the rows with A > @threshold.

Notice the use of @ before the variable name.


Example 4: String Matching Using Query

import pandas as pd

data = {'A': [1, 3, 5, 7],
        'B': [2, 4, 6, "bat"]}
df = pd.DataFrame(data)

# query the DataFrame for rows where 'B' matches a string
result = df.query('B == "bat"')

print(result)

Output

   A    B
3  7  bat

In this example, we performed string matching with query() to return the rows where B=="bat".