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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.