Reputation: 4418
How to translate SQL statement to Pandas:
select PolicyNumber,
Coverage
from ClaimsData
where AccidentState = 'AZ' and Coverage = 'Liability'
I know how select columns for single condition (AccidentState = 'AZ') but how can I add another condition?
ClaimsData[["PolicyNumber","Coverage","AccidentState"]] [ClaimsData["AccidentState"]=="AZ"] # and here I need to add another condition
Upvotes: 3
Views: 5110
Reputation: 862406
You can chain conditions by &
for bitwise AND
and filter by DataFrame.loc
rows by conditions (boolean indexing
) and columns by columns names in list
:
mask = (ClaimsData["AccidentState"]=="AZ") & (ClaimsData["Coverage"]=="Liability")
df = ClaimsData.loc[mask, ["PolicyNumber","Coverage"]]
Like @Sandeep Kadapa pointed in comments there is alternative with DataFrame.query
:
q = "AccidentState == 'AZ' & Coverage == 'Liability'"
df = ClaimsData.query(q)[["PolicyNumber","Coverage"]]
Upvotes: 9