![](/img/trans.png)
[英]How to add data from one dataframe to another using Pandas transpose?
[英]Using a list of dates to pull data from one DataFrame into another in Pandas
我在一个DataFrame中有一个非农业工资单的列表,dtype是Object。 我想将这些日期的eurusd_ask DataFrame中的所有5分钟数据拉到新的DataFrame中。 有人可以帮我弄清楚如何将日期列表传递给以下内容:
nfp_5min = eurusd_ask['2012-01-06']
我想使用日期列表“ nfp_dates”,而不是显示的“ 2012-01-06”特定日期
>>> nfp_dates.head(10)
Date
0 2012-01-06
1 2012-02-03
2 2012-03-09
3 2012-04-06
4 2012-05-04
5 2012-06-01
6 2012-07-06
7 2012-08-03
8 2012-09-07
9 2012-10-05
我的数据如下:
>>> eurusd_ask.head(10)
Open High Low Close Volume
Gmt time
2012-01-01 00:00:00 1.29496 1.29496 1.29496 1.29496 0.0
2012-01-01 00:05:00 1.29496 1.29496 1.29496 1.29496 0.0
2012-01-01 00:10:00 1.29496 1.29496 1.29496 1.29496 0.0
2012-01-01 00:15:00 1.29496 1.29496 1.29496 1.29496 0.0
2012-01-01 00:20:00 1.29496 1.29496 1.29496 1.29496 0.0
2012-01-01 00:25:00 1.29496 1.29496 1.29496 1.29496 0.0
2012-01-01 00:30:00 1.29496 1.29496 1.29496 1.29496 0.0
2012-01-01 00:35:00 1.29496 1.29496 1.29496 1.29496 0.0
2012-01-01 00:40:00 1.29496 1.29496 1.29496 1.29496 0.0
2012-01-01 00:45:00 1.29496 1.29496 1.29496 1.29496 0.0
所需结果
一个名为nfp_5min的DataFrame,它将具有nfp_dates的所有5分钟eurusd_ask数据。
可以使用numpy.isin
构建用于选择所需行的数组,其用法如下:
nfp_5min = eurusd_ask[np.isin(eurusd_ask.index.date, desired)]
eurusd_ask = pd.read_fwf(
StringIO(u"""
Gmt time Open High Low Close Volume
2012-01-01T00:00:00 1.29496 1.29496 1.29496 1.29496 0.0
2012-01-01T00:05:00 1.29496 1.29496 1.29496 1.29496 0.0
2012-01-01T00:10:00 1.29496 1.29496 1.29496 1.29496 0.0
2012-01-02T00:15:00 1.29496 1.29496 1.29496 1.29496 0.0
2012-01-02T00:20:00 1.29496 1.29496 1.29496 1.29496 0.0
2012-01-02T00:25:00 1.29496 1.29496 1.29496 1.29496 0.0
2012-01-03T00:30:00 1.29496 1.29496 1.29496 1.29496 0.0
2012-01-03T00:35:00 1.29496 1.29496 1.29496 1.29496 0.0
2012-01-03T00:40:00 1.29496 1.29496 1.29496 1.29496 0.0
2012-01-04T00:45:00 1.29496 1.29496 1.29496 1.29496 0.0"""),
header=1, parse_dates=['Gmt time']
).set_index('Gmt time')
nfp_dates= pd.to_datetime(['2012-01-01', '2012-01-02']).date
nfp_5min = eurusd_ask[np.isin(eurusd_ask.index.date, nfp_dates)]
print(nfp_5min)
Open High Low Close Volume
Gmt time
2012-01-01 00:00:00 1.29496 1.29496 1.29496 1.29496 0.0
2012-01-01 00:05:00 1.29496 1.29496 1.29496 1.29496 0.0
2012-01-01 00:10:00 1.29496 1.29496 1.29496 1.29496 0.0
2012-01-02 00:15:00 1.29496 1.29496 1.29496 1.29496 0.0
2012-01-02 00:20:00 1.29496 1.29496 1.29496 1.29496 0.0
2012-01-02 00:25:00 1.29496 1.29496 1.29496 1.29496 0.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.