Python Forum
Conditional If Statement: If value contains string then set another column equal to s
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Conditional If Statement: If value contains string then set another column equal to s
#1
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?
Reply


Messages In This Thread
Conditional If Statement: If value contains string then set another column equal to s - by Jack_Sparrow - Jun-15-2018, 11:42 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Pandas: summing columns conditional on the column labels ddd2332 0 2,110 Sep-10-2020, 05:58 PM
Last Post: ddd2332
  Convert dataframe string column to numeric in Python darpInd 1 2,297 Mar-14-2020, 10:07 AM
Last Post: ndc85430
  ' List[ Conditional statement ] ' What is this code supposed to do. Arindam 1 2,210 Nov-30-2018, 04:51 PM
Last Post: ichabod801
  Create selection box to pass string value based on uniques in Excel column sneakysnek 1 2,497 Nov-18-2018, 07:29 PM
Last Post: Stefanovietch
  RNG; One variable must not be equal to another DavidRobinsons 12 5,849 Oct-10-2018, 02:43 AM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020