1
df = pd.DataFrame({
    "Color": ["red", "red", "blue"],
    "Width": ["wide", "narrow", "narrow"],
    "Weight": [12, 12, 12],
})

   Color  Width   Weight
0  red    wide        12
1  red    narrow      12
2  blue   narrow      12

I'd like to add a new column EffWeight,

EffWeight = Weight if Color == 'red' and Width == 'wide' else 0

   Color  Width   Weight  EffWeight
0  red    wide        12         12
1  red    narrow      12          0
2  blue   narrow      12          0

How do I do that?

2 Answers 2

2

One option is to use a boolean condition and multiply:

df['EffWeight'] = (df['Color'].eq('red') & df['Width'].eq('wide')) * df['Weight']

another option is to use numpy.where:

import numpy as np
df['EffWeight'] = np.where(df['Color'].eq('red') & df['Width'].eq('wide'), df['Weight'], 0)

Output:

   Color   Width  Weight  EffWeight
0   red    wide      12         12
1   red  narrow      12          0
2  blue  narrow      12          0
Sign up to request clarification or add additional context in comments.

Comments

1
df['EffWeight'] = 0
df.loc[(df['Color']=='red') & (df['Width']=='wide'), 'EffWeight'] = df['Weight']

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.