将条件分配给数据框时出错

时间:2020-06-08 18:13:32

标签: python pandas dataframe

我正在尝试根据数据框的其他列对现有数据框的列进行操作。

例如:

if (df['col1']== 'this is') & (df['col2'] == 30) & (df['col3'] <= '2020-01-01'):
   df['Value'] = 0

在这里,我试图为满足Value语句中条件的if列的每个元素分配值0。

但是我得到以下错误:

系列的

真值不明确。使用a.empty,a.bool(),a.item(),a.any()或a.all()

2 个答案:

答案 0 :(得分:1)

使用np.where可获得更好的效果:

import numpy as np

df['Value'] = np.where(((df['col1']== 'this is') & (df['col2'] == 30) & (df['col3'] <= '2020-01-01')), 0, df['Value'])

答案 1 :(得分:1)

您可以使用.loc

upi://pay?pa=<VPA>&am=<AMOUNT>