How to filter a two columns with specific values in pandas

Viewed 55

I want to filter two columns Administration and State column and print the values where state is California.

df.head()

   R&D Spend  Administration  Marketing Spend       State     Profit
0  165349.20       136897.80        471784.10    New York  192261.83
1  162597.70       151377.59        443898.53  California  191792.06
2  153441.51       101145.55        407934.54     Florida  191050.39

I tried to filter the values but i get only the boolean values.How to print the values?

df.loc[:,['Administration','State']].isin(['California'])

    Administration  State
0   False   False
1   False   True
2   False   False
2 Answers

You can use any on axis=1 then boolean indexing:

df[df.loc[:,['Administration','State']].isin(['California']).any(1)]

Or for only 2 columns ['Administration','State']:

df.loc[df['State'].isin(['California']),['Administration','State']] #or.eq() for 1 value

If you only have one column , you can do eq

df[df[['Administration','State']].eq('California').any(1)]
Related