1

I wanted to select only those row which fulfill my condition.

options = ['apple', 'banana','cat','dog'] 
#selecting rows based on condition 
rslt_df = data[(data['Animal'] == "All") & data['Category'].isin(options)]
print(rslt_df)

But I am getting this error.

       #selecting rows based on condition
-----> rslt_df = data[(data['Animal'] == "All") & data['Category'].isin(options)]
       print(rslt_df)
       TypeError: invalid type comparison
freak7
  • 99
  • 12
  • Did you search first? This is a very common use case. You can start [here](https://stackoverflow.com/questions/17071871/select-rows-from-a-dataframe-based-on-values-in-a-column-in-pandas). Plus, how is your dataframe, what condition? is `All` a possible value in `Animal` column?. Elaborate please. – MikeMajara Sep 27 '19 at 05:04
  • How working `data[(data['Animal'].values == "All") & data['Category'].isin(options)]` ? – jezrael Sep 27 '19 at 05:05
  • Another idea - what is your pandas version? – jezrael Sep 27 '19 at 05:07
  • my pandas version is 0.25, Thanks for answering guys now its working. @jezrael – freak7 Sep 27 '19 at 05:14
  • Thanks for answering guys now its working. @MikaMajara – freak7 Sep 27 '19 at 05:15

2 Answers2

1

This error was in some old versions of pandas, solution is upgrade to last version and then use your original solution:

rslt_df = data[(data['Animal'] == "All") & data['Category'].isin(options)]
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
-1

If you provide an example DataFrame we can check why you have TypeError. But if you have the same datatypes this should work.

mask = (data['Animal'] == "All") & (data['Category'].isin(options))
new_df = df.loc[mask]
pesap
  • 1