学习中遇到的其他问题:
1. 填写以下内容,使得统计在2015年整年内(即1月1日至12月31日)微软股票涨价的每个月天数据。
list1 = []tmpdf = quotesdf['15/01/01':'15/12/31']for i in range(0, len(tmpdf)): list1.append(int(tmpdf.index[i][3:5]))tmpdf['____'] = list1print tmpdf[ tmpdf.close > temp._____]['month'].value_counts()
2. 以下哪一个选项能合并在2015年整年内(即1月1日至12月31日)微软股票收盘价最高的5天和最低的5天?
sorted = quotesdf.sort('close')pd.concat([sorted[:5],sorted[__:]])
3. 求微软公司2015年每个月股票收盘价的平均值。
In [1]: from matplotlib.finance import quotes_historical_yahoo_ochlIn [2]: from datetime import date,datetimeIn [3]: import pandas as pdIn [4]: start = date(2015,1,1) # 创建时间格式In [5]: end = date(2016,1,1)In [6]: quotes = quotes_historical_yahoo_ochl('MSFT',start,end)In [7]: fields = ('date','open','close','high','low','volume')In [8]: list1 = []In [9]: for i in range(0,len(quotes)): ...: x = date.fromordinal(int(quotes[i][0])) ...: y = datetime.strftime(x,'%m') # 仅表示为月份即可 ...: list1.append(y) ...: In [10]: quotesdf = pd.DataFrame(quotes,index=list1,columns=fields)In [11]: quotesdfOut[11]: date open close high low volume01 735600.0 44.210266 44.305014 44.930363 44.096567 27913900.001 735603.0 43.935490 43.897593 44.276590 43.821791 39673900.0...12 735963.0 54.543321 53.998276 54.689313 53.939876 26529600.0[252 rows x 6 columns]In [17]: quotesdf['month'] = list1 # 添加一列月份用于分组In [18]: quotesdfOut[18]: date open close high low volume month01 735600.0 44.210266 44.305014 44.930363 44.096567 27913900.0 0101 735603.0 43.935490 43.897593 44.276590 43.821791 39673900.0 01...12 735963.0 54.543321 53.998276 54.689313 53.939876 26529600.0 12[252 rows x 7 columns]In [20]: quotesdf.groupby('month').close.mean() # 分组,选列,计算Out[20]: month01 43.12443302 40.95677203 40.20391804 41.47768505 45.47229106 44.14587907 43.80754108 43.83889509 42.11415510 47.08288211 52.25287812 53.916431Name: close, dtype: float64
1. 在 ipython 下运行已经存储好的python代码:In [5]: run finance_data.py
1. 需要生成下图中的DataFrame