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

Avoid Alphabetical order on python finance download

我正在从 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

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

我就是这样做的。

"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]