0

I have a pandas DataFrame with a column 'categories'. That column has list as elements and each list contains some words. I want to find in how many lists the words "pizza" or "Italian" appear. The columns look like: screenshot how the columns look like

I tried the code below, but I am getting everything as False.

business.categories.apply(lambda x: ('pizza' in x) or ("Italian" in x))
JohanC
  • 71,591
  • 8
  • 33
  • 66
Sahil Swaroop
  • 81
  • 1
  • 4
  • 2
    Please post the sample of your dataframe as text in the question, not as a picture. See [How to make good pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – G. Anderson Jan 03 '20 at 20:19
  • Try `business.categories.apply(lambda x: {'italian', 'pizza'} < x)` – yatu Jan 03 '20 at 20:20
  • I am not able to re-create this behavior locally creating my own example from your screenshot. As @G.Anderson if you post a sample of the dataframe into the question the community will be able to provide further help – sedavidw Jan 03 '20 at 20:23
  • Lists in dataframes isn't really a good use case for pandas – juanpa.arrivillaga Jan 03 '20 at 21:08

1 Answers1

0

Dont use or use |

business['categories'].map(lambda x: ('pizza' in x) | ("Italian" in x))

pandas doesn't like and or or, use & or |

Something to note:

Make sure the values in your list don't have whitespaces, for example Italian and _Italian are different. If that's the case you have to do this in two pars

Cleaning

business['categories'] = business['categories'].map(lambda x: [i.strip() for i in x])

then map

business['categories'].map(lambda x: ('pizza' in x) | ("Italian" in x))

Kenan
  • 13,156
  • 8
  • 43
  • 50