简体   繁体   English

如何从列中选取值并将它们添加到数学函数中 (Pandas)

[英]How to pick values from column and add them to a mathematical function (Pandas)

I'm ttrying to pick all the values from a dataframe column I have and apply them to a mathematical function.我正在尝试从我拥有的数据框列中选择所有值并将它们应用于数学函数。 Heres how the data looks:以下是数据的外观:

   Year  %  PM
1  2002  3
2  2002  2.3

I am trying to apply this function :我正在尝试应用此功能:

M = 100000
t = (THE PERCENTAGE FROM THE DATAFRAME)/12
n = 15*12


PM = M*((t*(1+t)**n)/(((1+t)**n)-1))
print(PM)

And my goal is to do it to all the rows and append the value of each result to PM in the dF我的目标是对所有行执行此操作并将每个结果的值附加到 dF 中的 PM

You can just add the formula as a column directly to the DF, creating t_div_12 as a vector from the column as below:您可以将公式作为一列直接添加到 DF,从列中创建t_div_12作为向量,如下所示:

M = 100000
n = 15*12
t_div_12 = df["%"]/12
df["PM"] = M*((t_div_12 *(1+t_div_12 )**n)/(((1+t_div_12)**n)-1))
df['PM'] = df['%'].map(lambda t: M*(((t/12)*(1+(t/12))**n)/(((1+(t/12))**n)-1)))

First, I would avoid using constants, which are not repeated in the code.首先,我会避免使用在代码中没有重复的常量。 You can apply this function to your dataframe by using this code snippet:您可以使用以下代码段将此函数应用于您的数据框:

dF = pd.DataFrame([[2002, 3], [2002, 2.3]], columns=["Year", "%"])

dF['PM'] = 100000*((dF["%"]/12*(1+dF["%"]/12)**(15*12))/(((1+dF["%"]/12)**(15*12))-1))

It will give you:它会给你:

   Year    %            PM
0  2002  3.0  25000.000000
1  2002  2.3  19166.666667

暂无
暂无

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

相关问题 如何在熊猫中分组并从另一列添加值并计算它们 - How to groupby in pandas and add values from another column and count them 如何选择一列的一些值并用它们制作另一个? - How to pick some values of a column and make another one with them? Python Pandas - Dataframe - 根据另一列添加列,该列具有来自另外两列的数学运算 - Python Pandas - Dataframe - Add column depending on another column, which has a mathematical operation from another two columns 如何从函数中获取返回值并将它们放在数据框列上 - How to take returned values from a function and put them on a dataframe column 从每行的熊猫帧中提取唯一值并将它们添加到新列中 - Extract unique values from pandas frame per row and add them to a new column 仅当列值高于特定值时如何对列值进行数学运算,否则将其设置为1(以熊猫为单位)? - How to do mathematical operations on the column values only if they are above certain values, otherwise set it to 1 in pandas? 如何从一个数据框中的列中提取特定值并将它们附加到另一个数据框中的列中? - 熊猫 - How do you extract specific values from a column in one dataframe and append them to a column in another dataframe? - Pandas 如何将 Pandas 系列中的值添加到 Dataframe 列而不重复 - How to add values from a Pandas Series to a Dataframe column without duplicates 如何从Datareader pandas中挑选出单独的数值列? - how to pick out individual columns of numerical values from Datareader pandas? 如何从 pandas 中的拆分值拆分列并添加其他行? - How to split a column and add additional rows from the split values in pandas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM