我想基于日期在python中导入的excel文件中提取一些数据。 我希望能够提供开始日期和结束日期,并获取该特定时期的数据。 我尝试了不同的方法来安装pandas_datareader以使用以下代码,但我不能。
data = web.DataReader(dataset,start='', end='')
所以,这是我的代码。
import pandas as pd
import datetime
data = pd.read_excel('file.xlsx')
start = datetime.datetime(2009,1,1)
end = datetime.datetime(2018,1,1)
#reshape based on date
set_index = data.set_index('date')
如何解决此问题? :(
答案 0 :(得分:0)
> df %>% tidyr::gather(key="name",value="val",-"id")
id name val
1 id1 a -0.62645381
2 id2 a 0.18364332
3 id3 a -0.83562861
4 id4 a 1.59528080
5 id5 a 0.32950777
6 id6 a -0.82046838
7 id7 a 0.48742905
8 id8 a 0.73832471
9 id9 a 0.57578135
10 id10 a -0.30538839
...
输出:
import pandas as pd
import datetime
"""
ticker,date,closeunadj
ZF,2018-11-28,9.22
ZF,2018-11-27,9.16
ZF,2018-11-26,9.23
"""
df = pd.read_clipboard(sep=",", parse_dates=["date"]).set_index("date")
df
然后您可以使用 ticker closeunadj
date
2018-11-28 ZF 9.22
2018-11-27 ZF 9.16
2018-11-26 ZF 9.23
将索引子集化为所需的日期范围。
.loc
输出:
df.loc[(df.index < datetime.datetime(2018, 11, 28)) & (df.index > datetime.datetime(2018, 11, 26))]