Python基本数据统计(四)---- 其他问题

摘要:
学习中遇到的其他问题:1.填写以下内容,使得统计在2015年整年内微软股票涨价的每个月天数据。list1=[]tmpdf=quotesdf['15/01/01':'15/12/31']foriinrange:list1.appendtmpdf['____']=list1printtmpdf[tmpdf.close˃temp._____]['month'].value_counts()printtmpdf[tmpdf.close˃temp._____]['month'].value_counts()2.以下哪一个选项能合并在2015年整年内微软股票收盘价最高的5天和最低的5天?sorted=quotesdf.sortpd.concatpd.concat3.求微软公司2015年每个月股票收盘价的平均值。

学习中遇到的其他问题:

1.填写以下内容,使得统计在2015年整年内(即1月1日至12月31日)微软股票涨价的每个月天数据。

Python基本数据统计(四)---- 其他问题第1张Python基本数据统计(四)---- 其他问题第2张
list1 =[]
tmpdf = quotesdf['15/01/01':'15/12/31']
for i inrange(0, len(tmpdf)):
    list1.append(int(tmpdf.index[i][3:5]))
tmpdf['____'] =list1
print tmpdf[ tmpdf.close > temp._____]['month'].value_counts()
print tmpdf[ tmpdf.close > temp._____]['month'].value_counts()

2.以下哪一个选项能合并在2015年整年内(即1月1日至12月31日)微软股票收盘价最高的5天和最低的5天?

Python基本数据统计(四)---- 其他问题第3张Python基本数据统计(四)---- 其他问题第4张
sorted = quotesdf.sort('close')
pd.concat([sorted[:5],sorted[__:]])
pd.concat([sorted[:5],sorted[__:]])

3.求微软公司2015年每个月股票收盘价的平均值。

Python基本数据统计(四)---- 其他问题第5张Python基本数据统计(四)---- 其他问题第6张
In [1]: from matplotlib.finance importquotes_historical_yahoo_ochl

In [2]: from datetime importdate,datetime

In [3]: importpandas as pd

In [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 inrange(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]: quotesdf
Out[11]: 
        date       open      close       high        low       volume
01  735600.0  44.210266  44.305014  44.930363  44.096567   27913900.0
01  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 6columns]

In [17]: quotesdf['month'] = list1  #添加一列月份用于分组
In [18]: quotesdf
Out[18]: 
        date       open      close       high        low       volume month
01  735600.0  44.210266  44.305014  44.930363  44.096567   27913900.0    01
01  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 7columns]

In [20]: quotesdf.groupby('month').close.mean()  #分组,选列,计算
Out[20]: 
month
01    43.124433
02    40.956772
03    40.203918
04    41.477685
05    45.472291
06    44.145879
07    43.80754108    43.83889509    42.114155
10    47.082882
11    52.252878
12    53.916431Name: close, dtype: float64
quotesdf.groupby('month').close.mean()

1. 在 ipython 下运行已经存储好的python代码:In [5]: run finance_data.py

1. 需要生成下图中的DataFrame

Python基本数据统计(四)---- 其他问题第7张

免责声明:文章转载自《Python基本数据统计(四)---- 其他问题》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇12个提高Java程序员工作效率的工具Django:Admin,Cookie,Session下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

python图像处理之pyocr

使用pyocr类库进行ocr识别,其中tools为’Tesseract’ #!/usr/bin/env python #coding=utf-8 __author__ = 'zhangdebin' from PIL import Image import sys import pyocr tools = pyocr.get_available_to...

python文件和目录操作方法大全

一、python中对文件、文件夹操作时经常用到的os模块和shutil模块常用方法。1.得到当前工作目录,即当前Python脚本工作的目录路径: os.getcwd()2.返回指定目录下的所有文件和目录名:os.listdir()3.函数用来删除一个文件:os.remove()4.删除多个目录:os.removedirs(r“c:python”)5.检验给...

Python-字符串str和json格式的转换

str转json str转换为json格式,前提一定需要保证这个str的格式和json是一致的,即左边最外层是大括号,右边的最外层是大括号。如果不一致,推荐用正则进行拆分至和json格式一致1. 通过json.loads进行转换 import jsonstr = '{"key": "wwww", "word": "qqqq"}'j = json.loads...

使用Python爬虫爬取网络美女图片

代码地址如下:http://www.demodashi.com/demo/13500.html 准备工作 安装python3.6 略 安装requests库(用于请求静态页面) pip install requests -i https://mirrors.ustc.edu.cn/pypi/web/simple 安装lxml库(用于解析html文件) p...

python、pip、whl安装和使用

1 python的安装 首先,从python的官方网站 www.python.org下载需要的python版本,地址是这个: http://www.python.org/ftp/python/2.7.6/python-2.7.6.msi 然后,运行下载的MSI安装包,不需要更改任何默认设置,直接一路点“Next”即可完成安装: 默认会安装到C:Python...

【转载】 Visual Studio Code几款FTP插件使用总结

===================================================== 平时要维护类似wordpress这样的网站,然后虚拟主机又不支持远程仓的版本管理。总而言之,只能通过下载到本地修改后再通过FTP上传的情况,每次修改后都要开启FTP进行代码提交,相当繁琐。因此就诞生了一些编辑器的FTP插件,其实以前用sublime...