【发布时间】:2021-09-06 12:53:02
【问题描述】:
如果ColumnC中有“0”和字符串“pandas”,我想用平均值更新ColumnA [我存储在columnB中]
df['ColumnA'] = df.apply(lambda x: x['ColumnB'] if (x['ColumnA']==0 & x['ColumnC']=='pandas') else x['ColumnA'], axis=1)
我收到了这个错误
unsupported operand type(s) for &: 'int' and 'str'
请告诉我如何解决它
【问题讨论】:
-
使用
and而不是&:(x['ColumnA']==0 and x['ColumnC']=='pandas')&适用于熊猫系列,但由于您通过行应用x['ColumnA']是python 标量,因此您不能使用@ 987654328@.