user1893354
user1893354

Reputation: 5938

Pandas equivalent to SQL where

I'm new to Pandas and I am having some trouble. Basically I'm trying to implement the SQL query

select count(fraud),state
from table
where fraud='REJECT'
group by state

I have the following python code

df.groupby('State').size()

however, this does not restrict to only fraud=='REJECT'. I tried

fraud=df['fraud']=='REJECT'
fraud.groupby('State').size()

however this creates a key error for 'State'. So I think it boils down to I don't know how to implement an SQL 'where' in Pandas. Can someone help me out? Thanks

Upvotes: 3

Views: 3523

Answers (1)

roman
roman

Reputation: 117345

You can do it like this:

df[df['fraud'] == 'REJECT'].groupby('State').size()

example:

>>> df = pd.DataFrame({'fraud':['REJECT', 'ACCEPT', 'REJECT', 'REJECT'], 'State':['AZ', 'AZ', 'TX', 'TX']})
>>> df[df['fraud'] == 'REJECT'].groupby('State').size()
State
AZ       1
TX       2
dtype: int64

Upvotes: 3

Related Questions