[英]Problem using function that uses boolean verification (string) to create a new column in pandas
I have a database (df2) with the following structure to calculate profitability (or gains) for Brazilian fixed income assets:我有一个具有以下结构的数据库 (df2) 来计算巴西固定收益资产的盈利能力(或收益):
df2: enter image description here df2:在此处输入图像描述
For every type of assets (described in the column "Tipo") I need to make a different calculation to calculate their gains.对于每种类型的资产(在“Tipo”一栏中描述),我需要进行不同的计算来计算它们的收益。 Eg.: if its a CDB% the calculation is one, if CDBIPCA is another, etc.
例如:如果它是 CDB%,则计算是一个,如果 CDBIPCA 是另一个,等等。
So I build a function "rentabilidade" which checks what type of fixed income in the column "Tipo" and perform the calculation accordingly.因此,我构建了一个 function “rentabilidade”,它检查“Tipo”列中的固定收入类型并相应地执行计算。
The function is below: function如下:
def rentabilidade(tipo, taxa, dtAplic, dtResg, cnpj):
if tipo == 'Caixa':
rentAtivo = 0
elif tipo.item == "CDB%":
rentAtivo = rent_cdbper(taxa)
elif tipo.item == 'CDBPre':
rentAtivo = rent_pre(taxa)
elif tipo.item == 'CDBIPCA':
rentAtivo = rent_cdbipca(taxa)
elif tipo.item == 'CDB+':
rentAtivo = rent_cdimais(taxa)
elif tipo.item == 'LetraPre':
rentAtivo = rent_letra_pre(taxa, dtAplic, dtResg)
elif tipo.item == 'LetraIPCA':
rentAtivo = rent_letra_ipca(taxa, dtAplic, dtResg)
elif tipo.item == 'Letra%':
rentAtivo = rent_letra_per(taxa, dtAplic, dtResg)
elif tipo.item == 'Fundos':
rentAtivo = rent_fundo(cnpj)
else:
rentAtivo = "Error"
return rentAtivo
My goal is to create a new column "rentabilidade" with all gains calculated row by row.我的目标是创建一个新列“rentabilidade”,其中逐行计算所有收益。
However when I run the following code:但是,当我运行以下代码时:
df2["rentabilidade"] = rentabilidade(df2["Tipo"], df2["Taxa"], df2["Aplicação"], df2["Vencimento"], df2["CNPJ_Emissor"])
I get this error:我收到此错误:
ValueError: The truth value of a Series is ambiguous.
Use a.empty, a.bool(), a.item(), a.any() or a.all().
I believe the python code is comparing the entire series to the value in the function instead of doing one by one.我相信 python 代码正在将整个系列与 function 中的值进行比较,而不是逐一比较。
I was expecting to have a column with each value calculated accordingly to the type described in the column "Tipo" (all strings).我期望有一个列,其中每个值都根据“Tipo”列(所有字符串)中描述的类型进行计算。
In order to apply a function to all rows of a dataframe, you must use a lambda function. In your case:为了将 function 应用于 dataframe 的所有行,您必须使用 lambda function。在您的情况下:
df2["rentabilidade"] = df2.apply(lambda x: rentabilidade(x["Tipo"], x["Taxa"], x["Aplicação"], x["Vencimento"], x["CNPJ_Emissor"]), axis=1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.