1

I have a dataframe as:

Feature     Prediction 
Dell 12-4.  Grade 1
Dell 12-5.  Grade 1
Dell 10-4.  Grade 1
Dell 10-5.  Grade 0
Dell 12-6.  Grade 0

I would like to create a new column called 'True Outcome', which filters Feature Column for strings that starts with 'Dell 12-' and assigns Grade 0 to it otherwise Grade 1.

The expected output should look like this:

Feature     Prediction  True Outcome
Dell 12-4.  Grade 1.    Grade 0
Dell 12-5.  Grade 1.    Grade 0
Dell 10-4.  Grade 1.    Grade 1
Dell 10-5.  Grade 0.    Grade 1
Dell 12-6.  Grade 0.    Grade 0

3 Answers 3

2

To find if the column starts with the requested string, you can use

df.Feature.str.startswith('Dell 12-')

and the assignment can be

import numpy as np
df['Outcome'] = np.where(df.Feature.str.startswith('Dell 12-'), 'Grade 0', 'Grade 1')
Sign up to request clarification or add additional context in comments.

Comments

0

First create a column with all 1s - then set all the values where feature contains Dell-12

df["True Outcome"] = 1  #Create column
df.loc[df["features"].str.startswith("Dell 12") ,"True Outcome"]=0 #Set the values to 0

Comments

0

A better approach:

df['True Outcome'] = np.where(df['Feature'].str.startswith('Dell 12'), 'Grade 0', 'Grade 1')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.