熊猫:如何遍历两个不同的数据框

时间:2019-01-11 15:03:19

标签: python pandas

我有两个数据框: 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

我需要做的是使用df_b的输入来遍历dfspread ['spread']。我还必须计算公式。到目前为止,我尝试了以下方法:

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

但是它不起作用!有什么想法吗?谢谢!

2 个答案:

答案 0 :(得分:1)

这是工作吗?

我还使用index禁止了_,因为您不需要它

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

答案 1 :(得分:0)

尝试这个我的朋友:

# 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)