简体   繁体   English

使用 function 使用 boolean 验证(字符串)在 pandas 中创建新列的问题

[英]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.

相关问题 Python pandas 基于 boolean 行创建带有字符串代码的新列 - Python pandas create new column with string code based on boolean rows pandas 根据 boolean 值创建新列 - pandas create new column based on boolean value 如何使用 Pandas Python 中的 f 字符串在 function 中创建新列? - How to create new column in function using f-string in Pandas Python? 在 pandas 中使用 apply function 创建新列 TypeError: string indices must be integers - Using apply function in pandas to create a new column TypeError: string indices must be integers Python如何在pandas数据帧的[]括号内提取指定的字符串,并创建一个具有布尔值的新列 - Python How to extract specified string within [ ] brackets in pandas dataframe and create a new column with boolean values two conditional variable and one numerical variable lambda function in pandas dataframe to create a new boolean column - two conditional variable and one numerical variable lambda function in pandas dataframe to create a new boolean column 使用依赖于另一列的布尔值创建一个新的Pandas df列 - Create a new Pandas df column with boolean values that depend on another column 在 pandas 列上使用分隔符拆分字符串以创建新列 - String split using a delimiter on pandas column to create new columns 使用 pandas 字符串包含和循环创建新类别列 - create new category column using pandas string contains and loops Create new column using custom function pandas df错误 - Create new column using custom function pandas df error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM