根据anoth的索引从数据帧中查询行的子集

2024-03-19 07:48:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个5分钟欧元美元的数据框,还有一个非农场工资数据的数据框。我想创建一个新的dataframe,其中包含NFP日期的所有eursud 5分钟数据行。见下文:

#NFP Days (Dataframe #1)
nfp_dates = forex_calendar.loc[forex_calendar['Event'] == '(United States) Nonfarm Payrolls']

enter image description here #将csv文件导入Pandas dataframes并转换为Pandas datetime并设置为index(Dataframe#2)

^{2}$

enter image description here

所需数据帧(数据帧3) 我想要一个名为“eurusd_ask_nfp”的新数据帧,其中包含nfp日期(即2012-01-06、2012-02-03、2012-03-09等)的所有5分钟数据行(全天)

Open    High    Low Close   Volume
Gmt time                    
2012-01-06 00:00:00 1.23627 1.23627 1.23532 1.23553 860.47
2012-01-06 00:05:00 1.23553 1.23553 1.23509 1.23532 698.33
2012-01-06 00:10:00 1.23533 1.23535 1.23518 1.23533 338.22
2012-01-06 00:15:00 1.23533 1.23535 1.23511 1.23518 480.64
2012-01-06 00:20:00 1.23518 1.23525 1.23493 1.23508 551.73
2012-01-06 00:25:00 1.23509 1.23537 1.23501 1.23512 349.78
2012-01-06 00:30:00 1.23510 1.23520 1.23471 1.23481 659.66
... ... ... ... ... ...                 
2012-02-03 00:00:00 1.33271 1.33271 1.33210 1.33225 870.59
2012-02-03 00:05:00 1.33225 1.33248 1.33212 1.33227 943.04
2012-02-03 00:10:00 1.33226 1.33226 1.33189 1.33212 659.45
2012-02-03 00:15:00 1.33213 1.33272 1.33212 1.33268 788.32
2012-02-03 00:20:00 1.33265 1.33333 1.33264 1.33315 953.20
2012-02-03 00:25:00 1.33315 1.33322 1.33282 1.33286 678.37
2012-02-03 00:30:00 1.33286 1.33291 1.33260 1.33260 442.39
2012-02-03 00:35:00 1.33261 1.33293 1.33250 1.33268 621.53
2012-02-03 00:40:00 1.33268 1.33292 1.33257 1.33288 511.00
2012-02-03 00:45:00 1.33288 1.33300 1.33240 1.33240 554.76
2012-02-03 00:50:00 1.33240 1.33240 1.33191 1.33225 839.80
... ... ... ... ... ... 
2012-03-09 00:00:00 1.25739 1.25748 1.25710 1.25711 439.46
2012-03-09 00:05:00 1.25712 1.25732 1.25709 1.25715 388.29
2012-03-09 00:10:00 1.25715 1.25733 1.25711 1.25726 291.65
2012-03-09 00:15:00 1.25729 1.25731 1.25685 1.25692 626.85
2012-03-09 00:20:00 1.25692 1.25696 1.25662 1.25663 394.68
2012-03-09 00:25:00 1.25662 1.25677 1.25648 1.25652 302.73
2012-03-09 00:30:00 1.25652 1.25665 1.25645 1.25646 457.76
2012-03-09 00:35:00 1.25646 1.25659 1.25629 1.25633 423.00
2012-03-09 00:40:00 1.25633 1.25649 1.25632 1.25641 173.08
2012-03-09 00:45:00 1.25642 1.25671 1.25630 1.25665 263.99
... ... ... ... ... ... 
etc., etc.

Tags: 数据dataframepandasetcdayscalendarlocdates
2条回答

尝试一下,但是您应该能够使用Index.isin来查询日期匹配的所有记录。在

idx = nfp_dates.index
eurusd_ask_nfp = eurusd_ask[eurusd_ask.index.isin(idx)]

如果有人想知道答案,我就知道了,给你:

nfp_dates = forex_calendar.loc[forex_calendar['Event'] == '(United States) Nonfarm Payrolls']
nfp_dates = nfp_dates.reset_index()
nfp_dates = nfp_dates['Date'].dt.date
nfp_5min = eurusd_ask[np.isin(eurusd_ask.index.date, nfp_dates)]

这是我想要的

^{pr2}$

相关问题 更多 >