Skip to content

Pandas Querying Examples #13

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)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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