0

I need help I create this dataframe:

enter image description here

And i need to create a new column with this function:

def pgto(x, num):
    if x == "(i)":
        return num
    elif x == "(ii)":
        return 750000.00
    elif x == "(iii)":
        return num * 0.7
    else:
        return 3000000.00

then i aplicate a multiple loop for:

for x in df_final["Tipo"]:
    for num in df_final["Valor_float"]:
        df_final["Pgto"] = pgto(x, num)

But the result was it:

enter image description here

What i should do?

1
  • try printing the contents of the "Tipo" column and take note what type they are. Bc I think it applies an automatic cask which doesnt mean x and (i) equal. Also you might wanna do things via indexes rather them this for each style. Commented Feb 23, 2022 at 12:55

1 Answer 1

1

You are not correctly looping over rows, I'd suggest the following:

for idx, row in df_final.iterrows(): #Proper way to loop over rows
    # with the idx value (~row number), you can store the output value of your function
    df_final.loc[idx, "Pgto"] = pgto(row['Tipo'], row['Valor_float']) 
Sign up to request clarification or add additional context in comments.

1 Comment

Many tks! You are the guy!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.