SoFunction
Updated on 2025-03-03

Summary of the usage of query in pandas

1. Introduction to the pandas library

pandas is a very popular data processing library in Python. It provides a large number of data structures (such as Series and DataFrame) and data analysis tools, making data processing simple and efficient. In pandas,query()The method is a powerful function that allows the user to passString expressionsTo filter data in DataFrame.

2. Basics of query() method

query()Methods allow you to use string expressions toFilter rows of DataFrame. This expression is similar to the regular expression you use in Python, but it specifically targets the column names and values ​​of DataFrame.

Example 1: Basic usage

import pandas as pd

# Create a sample DataFramedata = {
    'A': [1, 2, 3, 4],
    'B': [5, 6, 7, 8],
    'C': ['p', 'q', 'r', 's']
}
df = (data)

# Use query() method to filter rows with column A greater than 2filtered_df = ('A > 2')
print(filtered_df)

Output:

   A  B  C
2  3  7  r
3  4  8  s

3. Advanced usage and skills

query()The method is not limited to simple comparison operations, you can also use logical operators (such as&|) and more complex expressions to filter data.

Example 2: Using logical operators

import pandas as pd

# Create a sample DataFramedata = {
    'A': [1, 2, 3, 4],
    'B': [5, 6, 7, 8],
    'C': ['p', 'q', 'r', 's']
}
df = (data)

# Filter rows with column A greater than 2 and column B less than or equal to 7filtered_df = ('A > 2 and B <= 7')
print(filtered_df)

Output:

   A  B  C
2  3  7  r

Example 3: Using String Method

import pandas as pd

# Create a sample DataFramedata = {
    'A': [1, 2, 3, 4],
    'B': [5, 6, 7, 8],
    'C': ['p', 'qu', 'r', 's']
}
df = (data)

# Filter rows starting with 'q' in column Cfiltered_df = ('("q")')
print(filtered_df)

Output:

   A  B   C
1  2  6  qu

4. Combined with other pandas functions

query()Methods can also be associated with other pandas functions (e.g.groupby()sort_values()etc.) Use in conjunction to perform more complex data operations.

Example 4: Combining groupby()

import pandas as pd

# Create a sample DataFramedata = {
    'A': [1, 1, 2, 2, 3, 3, 4, 4],
    'B': [5, 6, 6, 7, 7, 8, 8, 9],
    'C': ['p', 'q', 'r', 's', 'p', 'q', 'r', 's']
}
df = (data)

# Group by column A and filter the maximum value of column B in each groupgrouped_df = ('A').apply(lambda x: ('B == ()'))
print(grouped_df)

Output:

     A  B  C
A           
1 1  1  6  q
2 3  2  7  s
3 5  3  8  q
4 7  4  9  s

5. Summary

query()The method is a powerful and flexible tool in the pandas library that allows you to filter data in a DataFrame using easy-to-read string expressions. By combining logical operators and string methods, you can perform complex data filtering operations. However, when working with large datasets, you should be careful about performance issues and consider using other filtering methods.

This is the end of this article about the usage of query() in pandas. For more related pandas query() content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!