import pandas as pd
import numpy as np
def initialize(account):
stockList=get_all_securities('stock').index
log.info(stockList)
sortStocks=pd.DataFrame(columns=['code','price'])
for myStock in stockList:
priceHistory=history(myStock, ['close'], 1000, '1d', False, 'pre' )
current_to_median_ratio=priceHistory['close'][0]/np.median(priceHistory['close'])
sortStocks['code']=myStock
sortStocks['price']=current_to_median_ratio
log.info(sortStocks())
执行它,我得到:
2017-09-30 00:00:00 - INFOIndex(['000001.SZ', '000002.SZ', '000004.SZ', '000005.SZ', '000006.SZ',
'000007.SZ', '000008.SZ', '000009.SZ', '000010.SZ', '000011.SZ',
...
'603987.SH', '603988.SH', '603989.SH', '603990.SH', '603991.SH',
'603993.SH', '603996.SH', '603997.SH', '603998.SH', '603999.SH'],
dtype='object', length=3381)
2017-09-30 00:00:00 - INFO<class 'pandas.core.frame.DataFrame'>
2017-09-30 00:00:00 - INFOEmpty DataFrame
Columns: [code, price]
Index: []
sortStocks最终为空,为什么?
答案 0 :(得分:2)
使用.loc并尝试以下操作:
def initialize():
stockList=get_all_securities('stock').index
log.info(stockList)
sortStocks=pd.DataFrame(columns=['code','price'])
log.info(type(sortStocks))
# enumerate list so that you get an index
for idx, myStock in enumerate(stockList):
priceHistory=history(myStock, ['close'], 1000, '1d', False, 'pre' )
current_to_median_ratio=priceHistory['close'][0]/np.median(priceHistory['close'])
# use .loc to index a row
sortStocks.loc[idx, 'code'] = myStock
sortStocks.loc[idx, 'price'] = current_to_median_ratio