JIYIK CN >

Current Location:Home > Learning > PROGRAM > Python >

Pandas DataFrame DataFrame.query() function

Author:JIYIK Last Updated:2025/04/30 Views:

The pandas.DataFrame.query() method filters the rows of the caller DataFrame using the given query expression.


pandas.DataFrame.query()grammar

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

parameter

expr Filter rows based on query expressions
inplace Boolean. If true True, modify the caller DataFramedata in place
**kwargs Method keyword arguments

Return Value

If inplaceis True, return the filtered DataFrame; otherwise is None.


Example code: DataFrame.query()Method with a single condition

import pandas as pd

df = pd.DataFrame({'X': [1, 2, 3,],
                   'Y': [4, 1, 8]})
print("Original DataFrame:")
print(df)

filtered_df=df.query('X>1')
print("Filtered DataFrame:")
print(filtered_df)

Output:

Original DataFrame:
   X  Y
0  1  4
1  2  1
2  3  8
Filtered DataFrame:
   X  Y
1  2  1
2  3  8

It returns DataFrameonly the rows that satisfy the given query expression, that is, only the rows Xwhere the value of column is greater 1than .


DataFrame.query()Example code: Method when column names have spaces

Before applying this method to DataFrame, we have to make sure that the column names we are querying do not have any spaces in them.

If we have spaces in our column names, we can use backticks (`).

import pandas as pd

df = pd.DataFrame(
    {
        "X": [
            1,
            2,
            3,
        ],
        "Y": [4, 1, 8],
        "A B": [3, 5, 7],
    }
)
print("Original DataFrame:")
print(df)
filtered_df = df.query("`A B`>5")
print("Filtered DataFrame:")
print(filtered_df)

Output:

Original DataFrame:
   X  Y  A B
0  1  4    3
1  2  1    5
2  3  8    7
Filtered DataFrame:
   X  Y  A B
2  3  8    7

Here, the column name A Bhas spaces. To make a query expression for that column, we enclose the column name in backticks, otherwise an error will occur.


Example code: DataFrame.query()Method with multiple conditions

import pandas as pd

df = pd.DataFrame({'X': [1, 2, 3,],
                   'Y': [4, 1, 8]})
print("Original DataFrame:")
print(df)

filtered_df=df.query('X>1' and 'Y==1')
print("Filtered DataFrame:")
print(filtered_df)

Output:

Original DataFrame:
   X  Y
0  1  4
1  2  1
2  3  8
Filtered DataFrame:
   X  Y
1  2  1

If we want to filter based on multiple conditions DataFrame, we use andthe operator to combine multiple query expressions into a compound query expression.

It gives the rows in which the value of DataFramecolumn is greater than and the value of column is equal to .X1Y1

We can modify the original query()by setting after calling the method .inplace=TrueDataFrame

import pandas as pd
df = pd.DataFrame({'X': [1, 2, 3,],
                   'Y': [4, 1, 8]})
filtered_df=df.query('X>1' and 'Y==1',inplace=True)
print(df)

Output:

   X  Y
1  2  1

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

How to Apply a Function to a Column in a Pandas Dataframe

Publish Date:2025/04/30 Views:50 Category:Python

In Pandas, you can transform and manipulate columns and DataFrames using methods apply() such as transform() and . The desired transformation is passed to these methods as a function argument. Each method has its own subtle differences and

Finding the installed version of Pandas

Publish Date:2025/04/12 Views:190 Category:Python

Pandas is one of the commonly used Python libraries for data analysis, and Pandas versions need to be updated regularly. Therefore, other Pandas requirements are incompatible. Let's look at ways to determine the Pandas version and dependenc

KeyError in Pandas

Publish Date:2025/04/12 Views:81 Category:Python

This tutorial explores the concept of KeyError in Pandas. What is Pandas KeyError? While working with Pandas, analysts may encounter multiple errors thrown by the code interpreter. These errors are wide ranging and can help us better invest

Grouping and Sorting in Pandas

Publish Date:2025/04/12 Views:90 Category:Python

This tutorial explored the concept of grouping data in a DataFrame and sorting it in Pandas. Grouping and Sorting DataFrame in Pandas As we know, Pandas is an advanced data analysis tool or package extension in Python. Most of the companies

Plotting Line Graph with Data Points in Pandas

Publish Date:2025/04/12 Views:65 Category:Python

Pandas is an open source data analysis library in Python. It provides many built-in methods to perform operations on numerical data. Data visualization is very popular nowadays and is used to quickly analyze data visually. We can visualize

Converting Timedelta to Int in Pandas

Publish Date:2025/04/12 Views:124 Category:Python

This tutorial will discuss converting a to a using dt the attribute in Pandas . timedelta int Use the Pandas dt attribute to timedelta convert int To timedelta convert to an integer value, we can use the property pandas of the library dt .

Pandas fill NaN values

Publish Date:2025/04/12 Views:93 Category:Python

This tutorial explains how we can use DataFrame.fillna() the method to fill NaN values ​​with specified values. We will use the following DataFrame in this article. import numpy as np import pandas as pd roll_no = [ 501 , 502 , 503 , 50

Pandas Convert String to Number

Publish Date:2025/04/12 Views:147 Category:Python

This tutorial explains how to pandas.to_numeric() convert string values ​​of a Pandas DataFrame into numeric type using the method. import pandas as pd items_df = pd . DataFrame( { "Id" : [ 302 , 504 , 708 , 103 , 343 , 565 ], "Name" :

How to Change the Data Type of a Column in Pandas

Publish Date:2025/04/12 Views:139 Category:Python

We will look at methods for changing the data type of columns in a Pandas Dataframe, as well as options like to_numaric , , as_type and infer_objects . We will also discuss how to to_numaric use downcasting the option in . to_numeric Method

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial