Skip to content

Pandas Querying Examples #13

@s2t2

Description

@s2t2

Add some examples of using pandas dataframe.query, probably to the filtering rows section of the chapter about row operations.

https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.query.html

AI example:

import pandas as pd

# Create a sample DataFrame
df = pd.DataFrame({
    'name': ['john', 'david', 'anna', 'peter'],
    'country': ['USA', 'UK', 'USA', 'UK'],
    'age': [23, 45, 45, 30]
})

target_age = 45

# Query for rows where country is 'USA' and age is greater than 30
result = df.query("country == 'USA' and age > 30")

# Query using an external Python variable
result_var = df.query("age == @target_age")

print(result)
print(result_var)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions