简体   繁体   English

If else condition to compare 2 columns and insert a new column in Python pandas dataframe

[英]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 列并插入一个新列,其值是涉及其他一些列的计算,如上所示。

Use np.where :使用np.where

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.

相关问题 Python Pandas新的dataframe列有group by和condition - Python Pandas new dataframe column with group by and condition 如果满足条件,在 python 中使用 pandas 比较一行的 2 列和 output 一个新列 - Using pandas in python compare 2 columns of a row and output a new column if the condition is met 根据其他列的if-else填充pandas DataFrame的新列 - fill new column of pandas DataFrame based on if-else of other columns 比较两个 python pandas dataframe 字符串列以识别公共字符串并将公共字符串添加到新列 - Compare two python pandas dataframe string columns to identify common string and add the common string to new column 在python pandas中根据if-elif-else条件创建一个新列 - Creating a new column based on if-elif-else condition in python pandas 在 Python 中的 if else 条件下,如何在 Pandas 中创建新列 - How to create a New Column in Pandas on if else condition in Python 插入几个新列,其值基于 pandas 中 Dataframe 中的另一列 - Insert several new column with the values based on another columns in a Dataframe in pandas 将多列熊猫数据框与一列进行比较 - compare multiple columns of pandas dataframe with one column Pandas:将列与数据帧的所有其他列进行比较 - Pandas: Compare a column to all other columns of a dataframe Pandas 将多个列与数据框中的特定列进行比较 - Pandas compare multiple columns to a specific column in a dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM