Pandas 如何创建以日期为索引的数据框架
这是我的密码Pandas 如何创建以日期为索引的数据框架,pandas,Pandas,这是我的密码 import plotly.plotly as py import datetime import pandas import matplotlib.pyplot as plt import pandas.io.data as pd start = datetime.datetime(2016, 2, 1) end = datetime.datetime(2016, 2, 11) #raw = pd.DataReader("tjx", "yahoo", start, end
import plotly.plotly as py
import datetime
import pandas
import matplotlib.pyplot as plt
import pandas.io.data as pd
start = datetime.datetime(2016, 2, 1)
end = datetime.datetime(2016, 2, 11)
#raw = pd.DataReader("tjx", "yahoo", start, end)
rawy = pd.DataReader("tjx", "yahoo", start, end)['Low']
print rawy
print "========================"
columns = ['Low']
newDf = pd.DataFrame(columns=columns)
newDf = newDf.fillna(0)
#newDf[0] = rawy[0]
#newDf[0:1] = rawy[0:1]
#newDf.loc[0] = rawy.loc[0]
newDf.loc[0] = rawy[0]
print newDf
结果是这样的,
Date
2016-02-01 70.470001
2016-02-02 72.309998
2016-02-03 71.000000
2016-02-04 69.720001
2016-02-05 67.900002
2016-02-08 66.820000
2016-02-09 67.519997
2016-02-10 69.279999
2016-02-11 67.410004
Name: Low, dtype: float64
========================
Low
0 70.470001
如果查看结果的最后一行,它使用0作为索引,而不是原始数据帧的日期。那么如何纠正这个问题呢 它使用零作为索引,因为这是您分配给它的值。试试这个
newDf = pd.DataFrame(columns=columns)
>>> newDf
Empty DataFrame
Columns: [Low]
Index: []
newDf.ix[rawy.index[0]] = rawy[0] # Or newDf.loc[rawy.index[0]] = rawy[0]
newDf.ix[rawy.index[1]] = rawy[1]
>>> newDf
Low
2016-02-01 70.470001
2016-02-02 72.309998
如果你想索引过来,你必须分配它。这里有两种似乎有效的方法:
>>> newDf = pd.DataFrame(data=[rawy[0]], index=[rawy.index[0]], columns=columns)
>>> newDf
Low
2016-02-01 70.470001
或
非常感谢您的快速回答!嗯,如果我在问题代码的底部加上这个,我实际上会得到一个“***keyrerror:Timestamp('2016-02-01 00:00:00',tz=None)”。如果你还没有为newDf分配索引,你不认为你可以使用.ix吗?公平地说,我使用了
import pandas.io.data as web
和rawy=web.DataReader(“tjx”,“yahoo”,start,end)['Low']
。日期索引是自动分配的。我不确定该功能是否与pd.DataReader
不同。另外,我正在使用Pandas 0.17.1。你用的是哪个版本?我用的是Pandas 0.12.0,所以是的,可能是版本控制的问题。在我的示例中,rawy自动分配了索引,但是newDf通过“pd.DataFrame(columns=columns)”显然没有。在你的例子中,你不是说“在newDf中把这个值分配给索引为rawy.index[0](一个时间戳)的项吗?如果没有为newDf显式创建索引,那么它是如何工作的?注意这里我可能是pandas的中级-主要是问我是否遗漏了什么。我是说索引值为[Timestamp]的newDf被分配了rawy[0]
的值。如果该值在索引中尚不存在,则将创建该值。这将创建新的数据帧。现在如何为新值扩展该值?
>>> newDf = pd.DataFrame(rawy.head(1))
>>> newDf
Low
Date
2016-02-01 70.470001