简体   繁体   中英

pandas - updating a dataframe column with value of another column with if condition

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

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

((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.

使用and代替&并且还在==测试周围放置括号:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM