简体   繁体   English

如何将两个方程的最大值输入 pandas dataframe 列?

[英]How to input maximum value of two equations into pandas dataframe column?

I am trying to input into new column of dataframe maximum result of two equations based on different columns.我正在尝试输入 dataframe 基于不同列的两个方程的最大结果的新列。 Unfortunately I am having below error.不幸的是我有以下错误。 How should I change the code to make it work (I would like to stay with pandas library in this case)?我应该如何更改代码以使其工作(在这种情况下,我想继续使用 pandas 库)?

Prc1 Prc1 Prc2 Prc2 Price价格
1.5 1.5 2.5 2.5

df['Price'] = max(((df['Prc1'] - df['Prc2']) / df['Prc1']).abs(), ((df['Prc1'] - df['Prc2']) / ['Prc2']).abs())

ValueError: The truth value of a Series is ambiguous. ValueError:Series 的真值不明确。 Use a.empty, a.bool(), a.item(), a.any() or a.all()使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()

Use numpy.maximum :使用numpy.maximum

s = df['Prc1'] - df['Prc2']
df['Price'] = np.maximum((s / df['Prc1']).abs(), (s / df['Prc2']).abs())

Another idea with divide from right side by DataFrame.rdiv : DataFrame.rdiv从右侧划分的另一个想法:

df['Price'] = df[['Prc1','Prc2']].rdiv(df['Prc1'].sub(df['Prc2']), axis=0).abs().max(axis=1)

df = pd.DataFrame({'Prc1':[1,2,5], 'Prc2':[-5,8,-20]})


s = df['Prc1'] - df['Prc2']
df['Price1'] = np.maximum((s / df['Prc1']).abs(), (s / df['Prc2']).abs())
df['Price2'] = df[['Prc1','Prc2']].rdiv(df['Prc1'].sub(df['Prc2']),axis=0).abs().max(axis=1)
print (df)
   Prc1  Prc2  Price1  Price2
0     1    -5     6.0     6.0
1     2     8     3.0     3.0
2     5   -20     5.0     5.0

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Pandas 中的串联 dataframe 中找到列/行组合的最大值 - How to find the maximum value of a column/row combination in a concatenated dataframe in Pandas Pandas Dataframe 最大值列的名称 - Pandas Dataframe name of the column with maximum value 返回数据框中两列的最大值(Pandas) - Return the maximum value of two columns in a dataframe (Pandas) 将 Pandas dataframe 分组为两列, output 将最大列值指示到新列 - Group Pandas dataframe by two columns and output the maximum column value indication to new column 确定熊猫数据框中每列的最大值 - determine column maximum value per another column in pandas dataframe Pandas:如何在由另一列分组的列上获取具有最大值 value_count 的行作为数据框 - Pandas: how to get the rows that has the maximum value_count on a column grouping by another column as a dataframe 如何在熊猫数据框中输入列表作为列值? - How can I input a list as column value in a pandas dataframe? 如何在 pandas dataframe 的特定行和列中插入输入值 - How to insert a value from an input in a specific row and column in a pandas dataframe 如何查找两个 pandas dataframe 并从另一个 dataframe 列更新第一个 dataframe 列中的值? - how to do lookup on two pandas dataframe and update its value in first dataframe column from another dataframe column? 如何根据 Pandas dataframe 中的列值合并和居中两行? - How to merge and center two rows based on a column value in Pandas dataframe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM