避免按字母顺序排列在python财务下载

时间:2018-11-23 11:45:49

标签: python yahoo-finance

我正在从Yahoo Finance下载数据,并且正在按字母顺序下载数据。不像我发送列表。如何避免这种情况?

STOCKS= ['^TNX', '^VIX', 'SPY', 'QQQ', 'DIA', '^FTSE', '^GDAXI', '^HSI', '^N225',
    'XLC', 'XLY', 'XLP', 'XLE', 'XLF', 'XLV', 'XLI', 'XLB', 'XLRE', 'XLK', 'XLU',
     'USO', 'GLD', 'SIL', 'CPER', 'PALL', 'LIT', 'URA', 'REMX', 'OYB', 'CORN', 'CANE', 'TAGS', 'WEAT']



start_date = '2016-06-01'
end_date = '2018-06-01'

extras_data = fyf.download(STOCKS, start_date, end_date)['Close']
print(extras_data)

回来了什么

     CANE       CORN    ...       ^TNX       ^VIX

日期...
2016-05-31 11.50 22.139999 ... 1.834 14.190000 2016-06-01 11.56 22.610001 ... 1.846 14.200000 2016-06-02 11.85 22.549999 ... 1.811 13.630000 2016-06-03 12.13 22.760000 ... 1.704 13.470000

列按字母顺序排列,我想遵守列表顺序。

1 个答案:

答案 0 :(得分:3)

这就是我要做的。

"your code here"

#after pulling the data from fyf. This will check what
#actual stocks were pulled and put it in a list

sorted_columns = [STOCK for STOCK in STOCKS if STOCK in extras_data.columns]

#then I use those columns to re-sort the dataframe from fyf
print(extras_data.loc[:,sorted_columns])

#or you can store it back to itself
extras_data = extras_data.loc[:,sorted_columns]