Pandas Query (Filter Data) β df.query()
Filter data in Pandas using DataFrame.query
Pandas Query, the way to filter your data you havenβt heard of. Well I guess you have because youβre here.
Pandas DataFrame.query() will filter the rows of your DataFrame with a True/False (boolean) expression. This is super helpful when filtering your data.
Pseudo Code: Evaluate the expression give, return only the true rows.
Pandas Query
.query() is simple, but the magic lies in how creative you get with your expression. Check out a few examples below.
- expr β The string query that pandas will evaluate. Awesomely, you can also use variables within your string by starting them with β@β. Ex: β@myvariableβ
- inplace (Default: False) β Whether or not you want the DataFrame to be modified directly, or returned to you.
- **kwargs β You can go crazy by adding a ton of other parameters from pandas.eval(). I wonβt list them out here, explore for yourself!
Why isnβt this function more popular? Iβm guessing it is because Pandas education revolves around handing Series and doing series manipulation. Filtering the your dataframe the traditional way (by putting a boolean series in your df) forces students to manually munge their data.
Here is a traditional filter for reference
Now a code sample for .query()