jeudi 4 juin 2015

Python Pandas Dataframe Conditional If, Elif, Else

In a Python Pandas DataFrame, I'm trying to apply a specific label to a row if a 'Search terms' column contains any possible strings from a joined, pipe-delimited list. How can I do conditional if, elif, else statements with Pandas?

For example:

df = pd.DataFrame({'Search term': pd.Series(['awesomebrand inc', 'guy boots', 'ectoplasm'])})

brand_terms = ['awesomebrand', 'awesome brand']
footwear_terms = ['shoes', 'boots', 'sandals']

#Note: this does not work
if df['Search term'].str.contains('|'.join(brand_terms)):
  df['Label'] = 'Brand'
elif df['Search term'].str.contains('|'.join(footwear_terms)):
  df['Label'] = 'Footwear'
else:
  df['Label'] = '--'

Example desired output:

Search Term          Label
awesomebrand inc     Brand
guy boots            Footwear
ectoplasm            --

I've tried appending .any() to the ends of the contains() statements but it applies the Brand label to every row.

Most of the examples I come across are comparing if a column value == is equal to (not what I want) or are performing numeric comparisons, not text string comparisons.

Aucun commentaire:

Enregistrer un commentaire