Lucas Leite
Lucas Leite

Reputation: 1

multiple loop for in python

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?

Upvotes: 0

Views: 60

Answers (1)

Sebastien D
Sebastien D

Reputation: 4482

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']) 

Upvotes: 1

Related Questions