我正在尝试创建Pandas
DataFrame
,每列都是个别股票的百分比变化,按日期编制索引。我在修改百分比时遇到问题,将其添加到空DataFrame
并将该列命名为股票代码。
import pandas as pd
import pandas.io.data as web
from pandas import Series, DataFrame
stocks = {'xom': '2014-01-01', 'dvn': '2013-01-01', 'aapl': '2013-01-01'}
for key in stocks:
L = []
price = web.get_data_yahoo(key, stocks[key])['Adj Close']
change = price.diff().cumsum()
perChange = change / price.iloc[0]
到目前为止,一切都按计划进行......
L.append(perChange)
df = pd.DataFrame(L)
df.columns = keys
答案 0 :(得分:3)
将L
移到循环之外,因此不会在每次迭代时重新实例化。如果您将L
设为dict,那么dict的键将成为df = pd.DataFrame(L)
形成的DataFrame的列:
import pandas as pd
import pandas.io.data as web
stocks = {'xom': '2014-01-01', 'dvn': '2013-01-01', 'aapl': '2013-01-01'}
L = dict()
for stock, date in stocks.items():
price = web.get_data_yahoo(stock, date)['Adj Close']
change = price.diff().cumsum()
perChange = change / price.iloc[0]
L[stock] = perChange
df = pd.DataFrame(L)
print(df.head())
产量
aapl dvn xom
Date
2013-01-02 NaN NaN NaN
2013-01-03 -0.012622 0.017728 NaN
2013-01-04 -0.040125 0.040412 NaN
2013-01-07 -0.045772 0.040793 NaN
2013-01-08 -0.043203 0.033359 NaN