How to select multiple criteria from table in pandas

In below mentioned code we are getting count of different types of industries where country is ‘USA’. How can i put one more condition for example… country == USA and rank == 1. how to insert ‘rank’ condition in below code.

industry_usa = f500[‘industry’][f500[‘country’] == ‘USA’].value_counts().head()

industry_usa = f500[(f500['country'] == 'USA') & (f500['rank'] == '1')]['industry'].value_counts().head()

or

industry_usa  = f500.query("country == 'USA' & rank == 1")

Thanks for the reply… but i want to understand when do we use .loc , why we have not used .loc in this function since we are accessing columns with label name.

You can use the same code to use .loc

industry_usa = f500.loc[(f500['country'] == 'USA') & (f500['rank'] == '1'), ['industry']].value_counts().head()

If you make only a selection based on a Boolean mask, you can use just [], but if you plan to assign values to the selection, you will definitely need to .loc.

2 Likes