Skip to main content
 首页 » 编程设计

python之如何访问 Pandas 数据透视表的索引列

2024年11月01日9jackei

我想访问 pandas 数据透视表的日期/索引列,然后使用日期作为 matplotlib 轴以面向对象的方式绘制股票数据,我正在尝试弄清楚如何将日期放入自己的日期目的?谢谢你

import pandas as pd 
import yfinance as yf 
amazon = yf.download('amzn','2000-01-01','2019-09-26') 
amazon.head() 
 
            Open    High    Low Close   Adj Close   Volume 
Date                         
2000-01-03  81.50   89.56   79.05   89.38   89.38   16117600 
2000-01-04  85.38   91.50   81.75   81.94   81.94   17487400 
2000-01-05  70.50   75.12   68.00   69.75   69.75   38457400 
2000-01-06  71.31   72.69   64.00   65.56   65.56   18752000 
2000-01-07  67.00   70.50   66.19   69.56   69.56   10505400 
 

请您参考如下方法:

您可以使用 amazon.index:

print (amazon.index) 
DatetimeIndex(['1999-12-31', '2000-01-03', '2000-01-04', '2000-01-05', 
               '2000-01-06', '2000-01-07', '2000-01-10', '2000-01-11', 
               '2000-01-12', '2000-01-13', 
               ... 
               '2019-09-12', '2019-09-13', '2019-09-16', '2019-09-17', 
               '2019-09-18', '2019-09-19', '2019-09-20', '2019-09-23', 
               '2019-09-24', '2019-09-25'], 
              dtype='datetime64[ns]', name='Date', length=4965, freq=None) 

但是如果在 pandas 中绘制,例如通过 Series.plot不需要:

amazon['Open'].plot()