StackUser
StackUser

Reputation: 465

Create a pandas column with values from filtering string values from another column

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

Upvotes: 1

Views: 431

Answers (3)

Ami Tavory
Ami Tavory

Reputation: 76297

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')

Upvotes: 2

StackUser
StackUser

Reputation: 465

A better approach:

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

Upvotes: 0

CutePoison
CutePoison

Reputation: 5355

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

Upvotes: 0

Related Questions