Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Looking for values from the list among column values

So the problem I am facing that I am looking for a solution to do something like this:
(general example)

def categotizer(value):
   toys = ['ball', 'bear', 'lego']
   food = ['pizza', 'ice-cream', 'cake']
   if value in toys:
      return 'toys'
   if value in food:
      return 'food'
   else:
      return 'empty'

df['purchases_category'] = df['purchases'].apply(categorizer)

On a column which looks like the first one with result as the second column:
Table

In this exact method I am getting new column filled with ’empty’ values though I totally have examples from the lists. How could this possibly be fixed?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thanks.

>Solution :

Because in if value in toys: for example, value here is "red ball from…" and it’s not in the toys list. Same can be said for food. Instead, you might want to check the elements in toys/food against the value. Perhaps this would answer your concern?

import pandas as pd


def categorizer(value):
   toys = ['ball', 'bear', 'lego']
   food = ['pizza', 'ice-cream', 'cake']
   for toy in toys:
      if toy in value:
         return 'toys'
   for f in food:
      if f in value:
         return 'food'
   return 'empty'


df = pd.DataFrame({
   "purchases": ["red ball from the shop nearby", "chocolate cake cafe", "teddy bear extra"]
})
df['purchases_category'] = df['purchases'].apply(categorizer)
print(df)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading