0

I want to update ColumnA if it have "0" and string "pandas" in ColumnC with the mean value [which i stored in columnB]

df['ColumnA'] = df.apply(lambda x: x['ColumnB'] if (x['ColumnA']==0 & x['ColumnC']=='pandas') else x['ColumnA'], axis=1)

I am getting this error

unsupported operand type(s) for &: 'int' and 'str'

kindly advise how i can fix it

Falcon
  • 47
  • 6
  • 1
    Use `and` instead of `&`: `(x['ColumnA']==0 and x['ColumnC']=='pandas')` `&` is for pandas Series but since you are applying through rows `x['ColumnA']` is a python scalar, so you can't use `&`. – Psidom Jun 22 '21 at 23:45

2 Answers2

0

Put parentheses around your conditions, and as pointed out in the comments, use and instead of & for scalar comparison, e.g.

((x['ColumnA'] == 0) and (x['ColumnC'] == 'pandas'))

See this question on order of operations - the bitwise operator & takes precedence over the boolean operator ==

That said, you should consider using a vectorized operation:

df['ColumnA'] = df['ColumnB'].where(
    ((df['ColumnA'] == 0) & (df['ColumnC'] == 'pandas')),
    df['ColumnA'],
)

This will be faster than df.apply in nearly all cases.

Michael Delgado
  • 13,789
  • 3
  • 29
  • 54
0

Use and instead of & and also put brackets around the == tests:

df['ColumnA'] = df.apply(lambda x: x['ColumnB'] if (x['ColumnA']==0) and (x['ColumnC']=='pandas') else x['ColumnA'], axis=1)
SeaBean
  • 22,547
  • 3
  • 13
  • 25