Python Forum
Conditional If Statement: If value contains string then set another column equal to s - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: Conditional If Statement: If value contains string then set another column equal to s (/thread-10964.html)



Conditional If Statement: If value contains string then set another column equal to s - Jack_Sparrow - Jun-15-2018

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) in () 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?


RE: Conditional If Statement: If value contains string then set another column equal to s - Larz60+ - Jun-15-2018

df is a dictionary? Please show structure.


RE: Conditional If Statement: If value contains string then set another column equal to s - snippsat - Jun-15-2018

(Jun-15-2018, 01:52 PM)Larz60+ Wrote: df is a dictionary? Please show structure.
It's a DataFrame from Pandas.
@Jack_Sparrow same as i warned you about in your other post.