Query
Modifies a dataframe by retaining only the rows within the dataframe that met the desired crtieria. The conditions provide may implement one or more of the following:
- >, <, >=, <=, ==, !=
- starts with
- ends with
- contains
- >, <, >=, <=, ==, !=
- starts with
- ends with
- contains
Examples
Example 1 - Query with the Greater Than Operator
Selects all rows where the value in the Open column is greater than 150. This is a common pattern for identifying records above a specified cutoff.
#> Query Open > 150
AFLEFT
appleStockDf = appleStockDf[appleStockDf['Open'] > 150] AFRIGHT
Example 2 - Filter Rows Between Two Numeric Bounds
Retrieves rows where Low is greater than 100 and High is less than 110. Combines two numeric conditions using AND logic to isolate a range of interest.
#> Query Low > 100 and High < 110
AFLEFT
appleStockDf = appleStockDf[(appleStockDf['Low'] > 100) & (appleStockDf['High'] < 110)] AFRIGHT
Example 3 - Filter Rows with Mixed Conditions Using OR
Applies a compound filter where either a date is after 2022 or a numeric value in Low exceeds 300. Demonstrates how to combine conditions across different column types.
#> Query Date > 2022 or Low > 300
AFLEFT
appleStockDf['Date'] = pd.to_datetime(appleStockDf['Date'])
appleStockDf = appleStockDf[(appleStockDf['Date'].dt.year > 2022) | (appleStockDf['Low'] > 300)] AFRIGHT
Example 4 - Filter Rows by Substring Match in Text Columns
Returns rows where the Location column contains the substring "San". Useful for partial string matching in text-based data.
#> Query Location contains San
AFLEFT
bankTransactionsDf = bankTransactionsDf[bankTransactionsDf['Location'].astype('str').str.contains('San').fillna(False)] AFRIGHT
Example 5 - Filter Rows Where Text Column Ends With a Specific Value
Returns rows where the DeviceID column ends with a specific string. Demonstrates how to apply suffix-based matching for filtering text data.
#> Query DeviceID ends with endsWithValue
AFLEFT
bankTransactionsDf = bankTransactionsDf[bankTransactionsDf['DeviceID'].astype('str').str.endswith('endsWithValue').fillna(False)] AFRIGHT
Example 6 - Filter Rows with Greater Than and Less Than Operators with Mathematical Operations
In this example, we get the values that are within 10 percent above or below the median of the Apple stock.
#> Query High > .9 * appleStockDfMedian and High < 1.1 * appleStockDfMedian
AFLEFT
appleStockDf = appleStockDf[(appleStockDf['High'] > .9 * appleStockDfMedian) & (appleStockDf['High'] < 1.1 * appleStockDfMedian)] AFRIGHT