简体   繁体   English

如何使用线性方程 python 将新列计算为 dataframe

[英]How to calculate new column to a dataframe using a linear equation python

I have an existing df that looks something like this:我有一个看起来像这样的现有 df:

Current Price     Contract Cost     GP %     New Contract Cost
  30.19               16.01         .47            16.75
  84.22               60.90         .28            57.12
  95.16               58.54         .39            57.12

I want to add a new column to the df called 'New Price' which will be an updated price of the item based off of the new contract cost, keeping GP % the same.我想在 df 中添加一个名为“新价格”的新列,这将是基于新合同成本的项目更新价格,保持 GP % 不变。

If I were going to solve this on paper I would do GP % = (x - New Contract Cost) / x where x is the new price.如果我要在纸面上解决这个问题,我会做GP % = (x - New Contract Cost) / x其中 x 是新价格。

Example from df: .47 = (x -16.75)/x来自 df 的示例: .47 = (x -16.75)/x

x = ~31.60 x = ~31.60

So in this example 'New Price' for row one would be 31.60所以在这个例子中,第一行的“新价格”是 31.60

I have about 500 rows in the dataframe and don't want to manually calculate all of them.我在 dataframe 中有大约 500 行,不想手动计算所有行。

How would I go about constructing the code for this?我 go 如何为此构建代码? I'm very new to this and don't really know where to start.我对此很陌生,真的不知道从哪里开始。 I work in sales and want to be able to quantify price changes based on new contract costs if we keep GP % the same.我从事销售工作,如果我们保持 GP % 不变,我希望能够根据新合同成本量化价格变化。 Thanks in advance.提前致谢。

Try, using pandas intrinsic data alignment there is no need to loop nor use lambda functions:尝试使用 pandas 内部数据 alignment 无需循环,也无需使用 lambda 函数:

df['New Price'] = df['New Contract Cost']/(1-df['GP %'])

Output: Output:

   Current Price  Contract Cost  GP %  New Contract Cost  New Price
0          30.19          16.01  0.47              16.75  31.603774
1          84.22          60.90  0.28              57.12  79.333333
2          95.16          58.54  0.39              57.12  93.639344
   df['New Price'] = df['New Contract Cost'] / (1 - df['GP%'].astype(float))

Should create a column 'New Price' in df based on your current values.应该根据您当前的值在 df 中创建一个“新价格”列。 This just simplified the algebra to contain only your 'New Price' variable on the left side of the equation.这只是简化了代数,使其仅包含等式左侧的“新价格”变量。 This simplified the algebra so 'New Price' was alone on the left hand side of the equation allowing for a dataframe implementation of the solution.这简化了代数,因此“新价格”单独出现在等式的左侧,从而允许解决方案的 dataframe 实施。

It'd be optimal to rearrange your formula in the following way.最好按以下方式重新排列您的公式。

New Price = - (New Contract Cost / (GP % - 1)), where GP %.= 1.新价格 = -(新合同成本 /(GP % - 1)),其中 GP %.= 1。

Then you can implement this in Python with the following:然后您可以在 Python 中使用以下代码实现:

import pandas as pd

df = pd.DataFrame({"Current Price":[30.19,84.22,95.16],
                   "Contract Cost":[16.01,60.90,58.54],
                   "GP %":[0.47,0.28,0.39],
                   "New Contract Cost":[16.75,57.12,57.12]})

df["New Price"] = -(df["New Contract Cost"] / (df["GP %"] - 1))
   Current Price  Contract Cost  GP %  New Contract Cost  New Price
0          30.19          16.01  0.47              16.75  31.603774
1          84.22          60.90  0.28              57.12  79.333333
2          95.16          58.54  0.39              57.12  93.639344

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

相关问题 如何使用线性关系在python中计算新字段 - How to calculate a new field in python using a linear relationship 如何检索相应名称的值以在方程中将其计算为python中df中的新列 - How to retrieve the value of the corresponding name to calculate it in equation as a new column in the df in python 如何使用列值计算方程并使用 python 存储 output 值一个低于另一个? - How to calculate equation using the column values and store output values one below the other using python? 使用在单独的数据帧中使用 python 查找的值计算新数据帧列中的值 - Calculate values in a new dataframe column using values looked-up in a seperate dataframe using python 如何使用Python解决2个未知数的一个线性代数方程 - How to solve one linear algebra equation with 2 unknowns using Python 如何计算 Pandas dataframe 中的新“标准化”列? - How to calculate new “normalized” column in a Pandas dataframe? 放置方程来计算新列的值 - put equation to calculate value for new column 如何计算字符串中的方程,python - How to calculate an equation in a string, python 如何在 Python 中计算二次方程 - How to calculate a quadratic equation in Python 如何从 Dataframe 列 DOB(对象类型)计算年龄并使用 Pandas 在新列 Age 中填充年龄? - How to calculate age from a Dataframe column DOB(object type) and populate age in a new column Age using Pandas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM