Home > OS >  How to add a column to a pandas DataFrame based on other column values?
How to add a column to a pandas DataFrame based on other column values?

Time:04-07

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?

CodePudding user response:

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

CodePudding user response:

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