如何在python中对2个索引进行排序

时间:2019-02-19 20:41:08

标签: pandas

我正在尝试实现如下效果:

date      symbol    Open    High    low    close
2016-12-23  AAPL    804.6   809.9   800.5   809.1   
            CSCO    29.8    29.8    29.8    29.8
2016-12-27  AAPL    824.6   842.3   822.15  835.77  
            CSCO    29.32   29.9    29.3    29.85

这是我的代码:

from datetime import datetime
from iexfinance.stocks import get_historical_data
from pandas_datareader import data
import matplotlib.pyplot as plt
import pandas as pd
start =  '2014-01-01'
end = datetime.today().utcnow()
symbol = ['AAPL', 'MSFT']

out = pd.DataFrame()
datasets_test = []
for d in symbol:
    data_original = data.DataReader(d, 'iex', start, end)
    data_original['symbol'] = d
    data_original = data_original.set_index(['symbol'], append=True)
    out = pd.concat([out,data_original],axis=0) 
    out.sort_index()

print(out.tail(5))

这是我的结果:

                     open    high       low   close    volume
date       symbol                                            
2019-02-11 MSFT    106.20  106.58  104.9650  105.25  18914123
2019-02-12 MSFT    106.14  107.14  105.4800  106.89  25056595
2019-02-13 MSFT    107.50  107.78  106.7100  106.81  18394869
2019-02-14 MSFT    106.31  107.29  105.6600  106.90  21784703
2019-02-15 MSFT    107.91  108.30  107.3624  108.22  26606886

我试图在2个索引(日期+符号)中进行排序,并对使用排序感到困惑

谢谢!

0 个答案:

没有答案