我的第一个数据框(df1)如下:
Un s spread_bin gamma_star exp_star gamma_zero interval
0 0 0.000000 NaN NaN NaN NaN [0.0, 2.828e-05)
1 1 0.000110 A 1.5466 -1.210453e-07 1.5466 [8.485e-05, 0.0001131)
2 2 0.000308 A 1.5466 -1.007298e-07 1.5466 [0.0002828, 0.0003111)
3 3 0.000484 A 1.5466 -8.274816e-08 1.5466 [0.0004808, 0.0005091)
4 4 0.000601 A 1.5466 -7.075857e-08 1.5466 [0.0005939, 0.0006222)
5 5 0.000691 A 1.5466 -6.149985e-08 1.5466 [0.0006788, 0.000707)
第二个(df2):
time close high low open midprice s
0 2018-12-17 15:15:00 0.00318624 0.00318624 0.00318240 0.00318240 0.00318432 0.001206
1 2018-12-17 15:16:00 0.00319673 0.00319673 0.00318576 0.00318576 0.003191245 0.003438
2 2018-12-17 15:19:00 0.00319617 0.00319617 0.00319581 0.00319617 0.00319599 0.000113
3 2018-12-17 15:20:00 0.00318881 0.00319617 0.00318881 0.00319582 0.00319249 0.002305
4 2018-12-17 15:22:00 0.00319000 0.00319000 0.00319000 0.00319000 0.00319000 0.000000
5 2018-12-17 15:26:00 0.00319000 0.00319556 0.00319000 0.00319556 0.00319278 0.001741
6 2018-12-17 15:27:00 0.00318989 0.00319000 0.00318989 0.00319000 0.003189945 0.000034
我需要按如下方式更新df2 ['gamma']的新列:如果df2 ['s']以df1 ['interval']间隔之一结束,则选择相关的df1 [ ''gamma_star']并使用该值更新df2 ['gamma']!我不知道如何进行,有什么帮助吗?谢谢!
答案 0 :(得分:1)
我敢肯定有一种更好的方法可以做到这一点,但是一种快速的方法正在这样做:
def in_interval(value, lower, upper):
if lower <= value <= upper:
return True
else:
return False
df2['gamma'] = 0
for i, s in enumerate(df2['s']):
for j, interval in enumerate(df1['interval']):
if in_interval(s, interval[0], interval[1]):
df2.loc[i, 'gamma'] = df1.loc[j, 'gamma_star']