[英]If else condition to compare 2 columns and insert a new column in Python pandas dataframe
I want to compare 2 columns using the following if else condition and update a value in the new column using some formula.
我想使用以下 if else 条件比较 2 列,并使用某些公式更新新列中的值。
if df.loc[df['TF'] < df['PV']]:
df['Par_2']] = 0.01*(1.8 * df['PV'] + 32) - 2
elif(some other condition)
While I execute this I get the following error当我执行此操作时,出现以下错误
ValueError: The truth value of a DataFrame is ambiguous.
ValueError:DataFrame 的真值不明确。 Use a.empty, a.bool(), a.item(), a.any() or a.all().
使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。
Can any one help what can I do to resolve this error.任何人都可以帮助我做些什么来解决这个错误。 Or is there any other way to compare 2 columns and insert a new column whose value is calculation involving some other columns as shown above.
或者是否有任何其他方法来比较 2 列并插入一个新列,其值是涉及其他一些列的计算,如上所示。
df['Par_2']=np.where(df['TF'] < df['PV'], 0.01*(1.8 * df['PV'] + 32) - 2, 'enter here sencond formule')
or numpy.select :或numpy.select :
conditions=[df['TF'] < df['PV'],df['TF'] == df['PV'],df['TF'] > df['PV']] #for example
values=[0.01*(1.8 * df['PV'] + 32) - 2, 7, 10] #for example
df['Par_2']=np.select(conditions,values)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.