1

Lets say I have a list

vowels= ['a','e','i','o','u']

I have a pandas dataframe

id       alphabet
 1         b
 2         a
 3         c
 4         i

I need output like

id       alphabet  label
 1         b         
 2         a       vowel
 3         c
 4         i       vowel

I tried

df.loc[df.alphabet in vowels, 'label'] = "vowel"

This gives me a error. how do i do this?

bananagator
  • 551
  • 3
  • 8
  • 26
  • 2
    df.loc[df['alphabet'].isin(vowels), 'activity'] = "vowel". Make sure alphabet column has strings for this to work. Secondly, – Ajay Shah Aug 10 '18 at 16:58
  • 1
    Your code is right, see this duplicate on how to use `in` logic in dfs (use `isin` and not `in`): https://stackoverflow.com/questions/19960077/how-to-implement-in-and-not-in-for-pandas-dataframe – rafaelc Aug 10 '18 at 17:01

1 Answers1

2

I think need function Series.isin for condition.

If need missing values with vowel use:

df.loc[df.alphabet.isin(vowels), 'activity'] = "vowel"
print (df)
   id alphabet activity
0   1        b      NaN
1   2        a    vowel
2   3        c      NaN
3   4        i    vowel

Or if need empty values and vowel use numpy.where:

df['activity'] = np.where(df.alphabet.isin(vowels), 'vowel', '')
print (df)
   id alphabet activity
0   1        b         
1   2        a    vowel
2   3        c         
3   4        i    vowel
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252