如何创建以日期为索引的pandas数据框

时间:2016-02-13 23:20:17

标签: 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)
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作为索引,而不是原始数据框中的日期。那么如何解决这个问题呢?

2 个答案:

答案 0 :(得分:1)

它使用零作为索引,因为这是您为其分配的值。试试这个。

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

答案 1 :(得分:1)

如果你希望索引过来,你必须分配它。以下两种方法似乎有效:

>>> newDf = pd.DataFrame(data=[rawy[0]], index=[rawy.index[0]], columns=columns)
>>> newDf
                  Low
2016-02-01  70.470001

>>> newDf = pd.DataFrame(rawy.head(1))
>>> newDf
                   Low
 Date
 2016-02-01  70.470001