本文整理汇总了Python中pandas.io.data.DataReader.to_frame方法的典型用法代码示例。如果您正苦于以下问题:Python DataReader.to_frame方法的具体用法?Python DataReader.to_frame怎么用?Python DataReader.to_frame使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pandas.io.data.DataReader
的用法示例。
在下文中一共展示了DataReader.to_frame方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: DataReader
# 需要导入模块: from pandas.io.data import DataReader [as 别名]
# 或者: from pandas.io.data.DataReader import to_frame [as 别名]
# if first argument is a list, this returns a panel
p = DataReader(tickers, 'yahoo', start, end)
print('Type:', type(p))
print('Dimensions:', p.shape)
print('Axis labels:\n', p.describe, sep='')
#%%
close = p['Adj Close']
c = close.stack().sortlevel(level=1).swaplevel(0,1)
c
#%%
# another version: to_frame does the stacking automatically
# can select variable at the start or the end
df = p.to_frame().swaplevel(0,1).sortlevel(level=0)['Adj Close']
df
#%%
"""
Ju's version (stripped down)
"""
d = {}
for ticker in tickers: #symbols_list:
d[ticker] = DataReader(ticker, "yahoo", start, end)
pan = pd.Panel(d)
df = pan.minor_xs('Adj Close')
df
#df.to_csv("yahoo closing price value.csv")
示例2: DataReader
# 需要导入模块: from pandas.io.data import DataReader [as 别名]
# 或者: from pandas.io.data.DataReader import to_frame [as 别名]
from pandas.io.data import DataReader
import datetime
# small test sample
start = datetime.datetime(2012, 1, 1)
end = datetime.datetime(2015, 12, 31)
tickers = ['aapl', 'goog']
# if first argument is a list, this returns a panel
p = DataReader(tickers, 'yahoo', start, end)
print('Type:', type(p))
print('Dimensions:', p.shape)
print('Axis labels:\n', p.describe, sep='')
#%%
# convert to df, shift tickers to columns
df = p.to_frame().unstack('minor')['Adj Close']
df.head(20)
#%%
# convert to monthly (first day of month)
dm = df.resample('MS')
dm.head(20)
#%%
# percent change based on start of month
test = dm.head()
test.pct_change().shift(-1)