首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在pandas python中传递函数(外部编写)并分配给列?

如何在pandas python中传递函数(外部编写)并分配给列?
EN

Stack Overflow用户
提问于 2020-07-29 11:10:21
回答 2查看 246关注 0票数 0
代码语言:javascript
复制
def calc1(tentative_valuations, previous_buying_price):
  return tentative_valuations - previous_buying_price

#mortgage calculator
def calc2(loanper, previous_buying_price):
  return loanper * previous_buying_price



#indicative paid interest to date **REQUIRES ACCESSOR** ###################
calc3 = int(lookups((ym_between(b, a)[0]), (ym_between(b, a)[1]), 'Cum Interest Paid'))


# #net indicative capital gain today #######################################
calc4 = calc1(tentative_valuations, previous_buying_price) - calc3
#print(calc4)


# #to calculate psf before
def calc5(previous_buying_price, area):
  return previous_buying_price/area

# #to calculate psf after
def calc6(tentative_valuations, area):
  return tentative_valuations/area


#print(calc5(previous_buying_price,area))
#print(calc6(tentative_valuations, area))

# #annualised ##########################################
def calc7():
  return round((((calc6(tentative_valuations, area)/calc5(previous_buying_price, area))**(1/(ym_between(b,a)[0]+ ym_between(b,a)[1]/12))) - 1)*100,2)
#no problem
#print(calc7())

#indicative paid principal to date
calc8 = int(lookups((ym_between(b, a)[0]), (ym_between(b, a)[1]), 'Cumulative Paid Principal'))

#indicative balance 
calc9 = (loanper * previous_buying_price) - calc8
代码语言:javascript
复制
  #create the dataframe.
    df2 = pd.DataFrame(columns=['Gross Indicative Capital Gain Today', 'Net Indicative Capital Gain Today','Indicative Average Gross Annual Capital Gain', 'Indicative Paid Principal to Date ', 'Indicative Paid Interest to Date',
                        'Indicative Balance'])
    df2['Gross Indicative Capital Gain Today'] = calc1(tentative_valuations, previous_buying_price)
    df2['Net Indicative Capital Gain Today'] = calc3
    df2['Indicative Average Gross Annual Capital Gain'] = calc7()
    df2['Indicative Paid Principal to Date'] = calc8
    df2['Indicative Paid Interest to Date'] = calc3
    df2['Indicative Balance'] = calc9
    df2.head()

当我调用df2时,似乎只出现了标题?所有的计算要么是变量,要么是有输出的函数。刚接触熊猫(我的第二天学习),非常感谢你们的帮助,谢谢你们:)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-07-29 11:24:47

您不能将诸如int、bool之类的类型分配给数据框中的特定列,就像您在数组中将其包围在如下数组中一样:

代码语言:javascript
复制
df2['Indicative Paid Principal to Date'] = [calc8]

如果要向新行添加值,可以执行以下操作

代码语言:javascript
复制
new_values = [calc1(tentative_valuations, previous_buying_price), calc3, calc7, calc8, calc3, calc9]
df2.loc[0] = new_values
票数 0
EN

Stack Overflow用户

发布于 2020-07-29 11:20:24

直接调用df2不会做任何事情。

将最后一行更改为:

代码语言:javascript
复制
print(df2)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63145767

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档