vendredi 15 juin 2018

Conditional If Statement: If value contains string then set another column equal to string

I have a column 'original_title', where I have different film titles i.a. all films of Star Wars (+ the name of the episode) and Star Trek (+ the name of the episode). I want to create one column which will show me only 'star trek' (without the name of episode), 'star wars' and 'na'.

This is my code for the new column:

df['Trek_Wars'] = pd.np.where(df.original_title.str.contains("Star Wars"), "star_wars", 
              pd.np.where(df.original_title.str.contains("Star Trek"), "star_trek"))

However, it doesn't work

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-33-5472b36a2193> in <module>()
      1 df['Trek_Wars'] = pd.np.where(df.original_title.str.contains("Star Wars"), "star_wars",
----> 2                    pd.np.where(df.original_title.str.contains("Star Trek"), "star_trek"))

ValueError: either both or neither of x and y should be given

What should I do?

Aucun commentaire:

Enregistrer un commentaire