Improving the readability of Boolean indexing with the query method
Boolean indexing is not necessarily the most pleasant syntax to read or write, especially when using a single line to write a complex filter. pandas has an alternative string-based syntax through the DataFrame query method that can provide more clarity.
This recipe replicates the earlier recipe in this chapter, Translating SQL WHERE clauses, but instead takes advantage of the .query
method of the DataFrame. The goal here is to filter the employee data for female employees from the police or fire departments who earn a salary of between 80 and 120 thousand dollars.
How to do it…
- Read in the employee data, assign the chosen departments, and import columns to variables:
>>> employee = pd.read_csv("data/employee.csv") >>> depts = [ ... "Houston Police Department-HPD", ... "Houston Fire Department (HFD)", ... ] >>> select_columns...