Viktor.w
Viktor.w

Reputation: 2297

Pandas: how to iterate through two different dataframes

I have two dataframes: df_b:

Bin         A     B     C   Proba-a   Proba-b   Proba-c    gamma
CPB%                                                            
0.00000     0    57  1728  1.000000  0.996368  0.926577  0.00000
0.00100     0  1579  1240  1.000000  0.895743  0.873890  0.00100
0.00200  1360   488   869  0.869532  0.864644  0.836966  0.00200

dfspread:

      spread Bin
0   0.000001   A
1   0.000002   A
2   0.000003   A
3   0.000004   A
4   0.000005   B
5   0.000006   B

What I need to do it to iterate through dfspread['spread'] using input from df_b. I also have to compute formula. What I tried so far is the following:

f= 0.00000001
max_exp = []
for index, row in dfspread.iterrows():
    for index,row in df_b.iterrows():
            exp = row['Proba-a']*(row['gamma']*row['spread']*(1+f)-(f+f))
            max_exp.append(float(exp))

But it does not work! Any idea on this? Thanks!

Upvotes: 0

Views: 633

Answers (2)

Lucas Hort
Lucas Hort

Reputation: 854

Try this my friend:

# Spread values to an array
spread_values = [row['spread'] for index, row in dfspread.iterrows()]

f= 0.00000001
max_exp = []
for index,row in df_b.iterrows():
    for spread in spread_values:
        exp = row['Proba-a']*(row['gamma']*spread*(1+f)-(f+f))
        max_exp.append(float(exp))
print(max_exp)

Upvotes: 0

Filipe Aleixo
Filipe Aleixo

Reputation: 4244

Does this do the job?

I also suppressed index using _, since you don't need it

f= 0.00000001
max_exp = []
for _, row1 in dfspread.iterrows():
    for _,row2 in df_b.iterrows():
            exp = row2['Proba-a']*(row2['gamma']*row1['spread']*(1+f)-(f+f))
            max_exp.append(float(exp))

Upvotes: 1

Related Questions