Reputation: 3013
This is my code,
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
The result is like this,
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
If you look at the last line of result, it's using 0 as index, not date from the original data frame. So how to correct this please ?
Upvotes: 3
Views: 5706
Reputation: 109686
It is using zero as the index because that is the value you assigned to it. Try this instead.
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
Upvotes: 1
Reputation: 423
If you want the index to come over, you've got to assign it. Here's two methods that seem to work:
>>> newDf = pd.DataFrame(data=[rawy[0]], index=[rawy.index[0]], columns=columns)
>>> newDf
Low
2016-02-01 70.470001
or
>>> newDf = pd.DataFrame(rawy.head(1))
>>> newDf
Low
Date
2016-02-01 70.470001
Upvotes: 2