if statement - Python Pandas Dataframe Conditional If, Elif, Else -


in python pandas dataframe, i'm trying apply specific label row if 'search terms' column contains possible strings joined, pipe-delimited list. how can conditional if, elif, else statements 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: 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() ends of contains() statements applies brand label every row.

most of examples come across comparing if column value == equal (not want) or performing numeric comparisons, not text string comparisons.

here's 1 way it, using str.contains() , np.where()

in [26]: np.where(df['search term'].str.contains('|'.join(brand_terms)),         'brand',          np.where(df['search term'].str.contains('|'.join(footwear_terms)),              'footwear',              '--'))  out[26]: array(['brand', 'footwear', '--'],       dtype='|s8') 

which can assign df['label'] like

in [27]: df['label'] = np.where(df['search term'].str.contains('|'.join(brand_terms)),    ....:               'brand',    ....:               np.where(df['search term'].str.contains('|'.join(footwear_terms)),    ....:                       'footwear',    ....:                       '--'))  in [28]: df out[28]:         search term     label 0  awesomebrand inc     brand 1         guy boots  footwear 2         ectoplasm        -- 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -