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
#2
df is a dictionary? Please show structure.
Reply
#3
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Pandas - Creating additional column in dataframe from another column Azureaus 2 97 Jan-11-2021, 09:53 PM
Last Post: Azureaus
  Pandas: summing columns conditional on the column labels ddd2332 0 460 Sep-10-2020, 05:58 PM
Last Post: ddd2332
  Assigning Column nunique values to another DataFrame column Pythonito 0 377 Jun-25-2020, 05:04 PM
Last Post: Pythonito
  Pandas - Dynamic column aggregation based on another column theroadbacktonature 0 471 Apr-17-2020, 04:54 PM
Last Post: theroadbacktonature
  Convert dataframe string column to numeric in Python darpInd 1 670 Mar-14-2020, 10:07 AM
Last Post: ndc85430
  ' List[ Conditional statement ] ' What is this code supposed to do. Arindam 1 832 Nov-30-2018, 04:51 PM
Last Post: ichabod801
  Create selection box to pass string value based on uniques in Excel column sneakysnek 1 1,017 Nov-18-2018, 07:29 PM
Last Post: Stefanovietch
  RNG; One variable must not be equal to another DavidRobinsons 12 2,084 Oct-10-2018, 02:43 AM
Last Post: micseydel
  How to extract previous and current rows if their value is equal SriRajesh 5 1,654 Jun-18-2018, 08:09 PM
Last Post: volcano63
  linspace not dividing equal intervals sheel 0 1,148 Jan-16-2018, 04:28 PM
Last Post: sheel

Forum Jump:

User Panel Messages

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