python index out of bounds_使用python中遇到的坑
这周开始学习python的入门学习。
首先安装数据分析环境,Anaconda和Jupyter notebook已成为数据分析的标准环境。简单来说,Anaconda是包管理器和环境管理器,Jupyter notebook 可以将数据分析的代码、图像和文档全部组合到一个web文档中。 安装这两个环境中遇到问题可参考以下知乎文章:
初学python者自学anaconda的正确姿势是什么??www.zhihu.comjupyter notebook 可以做哪些事情?www.zhihu.com
作为入门教程,推荐菜鸟教程网上的python3教程Python3 教程 | 菜鸟教程www.runoob.com
,分为基础教程和高级教程2个部分,可以快速的掌握python3的语法、结构等。
遇到问题推荐使用世界上最大的搜索引擎查找技术问题UOL Buscabusca.uol.com.br
也可以在Stack Overflow上搜索,它是一个与程序相关的IT技术问答网站。用户可以在网站免费提交问题,浏览问题,索引相关内容,在创建主页的时候使用简单的HTML。在问题页面,不会弹出任何广告,销售信息,JavaScript 窗口等。
以下是使用python对GAFATA 6只股票做得初步分析,分析参考了知乎李玉竹的文章,原文如下:李玉竹:数据分析之简易分析股票走势zhuanlan.zhihu.com
import matplotlib.pyplot as plt
import pandas as pd
pd.core.common.is_list_like = pd.api.types.is_list_like
#pdr0.7版本会修复该问题
from pandas_datareader import data
import pandas_datareader as pdr
pdr.__version__
#ImmediateDeprecationError raise when trying to get data from yahoo
#通过导入fix_yahoo_finance包解决
import fix_yahoo_finance as yf
yf.pdr_override()
#注意腾讯股票代码不是HK0700
gafataDict = {'谷歌':'GOOG','亚马逊':'AMZN','Facebook':'FB','苹果':'AAPL','阿里巴巴':'BABA','腾讯':'0700.HK'}
start_data = '2017-01-01'
end_data = '2018-08-01'
GOOGDf = data.get_data_yahoo(gafataDict['谷歌'],start_data,end_data)
GOOGDf.head()
[*********************100%***********************] 1 of 1 downloaded
# GOOGDf.describe()
AMZNDf = data.get_data_yahoo(gafataDict['亚马逊'],start_data,end_data)
AMZNDf.head()
[*********************100%***********************] 1 of 1 downloaded
FBDf = data.get_data_yahoo(gafataDict['Facebook'],start_data,end_data)
FBDf.head()
[*********************100%***********************] 1 of 1 downloaded
AAPLDf = data.get_data_yahoo(gafataDict['苹果'],start_data,end_data)
AAPLDf.head()
[*********************100%***********************] 1 of 1 downloaded
BABADf = data.get_data_yahoo(gafataDict['阿里巴巴'],start_data,end_data)
BABADf.head()
[*********************100%***********************] 1 of 1 downloaded
#try:
HK00700Df = data.get_data_yahoo(gafataDict['腾讯'],start_data,end_data)
HK00700Df.head()
#except ValueError:
# pass
[*********************100%***********************] 1 of 1 downloaded
plt.figure(figsize=(10,5))#宽度和高度的单位为英寸
plt.plot(GOOGDf['Close'],color='blue')
plt.title('Google')
plt.xlabel('Date')
plt.ylabel('Closing price')
plt.grid(True)
plt.show()
plt.figure(figsize=(10,5))#宽度和高度的单位为英寸
plt.plot(HK00700Df['Close'],color='blue')
plt.title('腾讯',fontproperties='SimHei')#显示中文可用fontproperties解决
plt.xlabel('Date')
plt.ylabel('Closing price')
plt.grid(True)
plt.show()
plt.figure(figsize=(10,5))#宽度和高度的单位为英寸
plt.plot(GOOGDf['Close'],color='blue',label='google')
plt.plot(AMZNDf['Close'],color='Green',label='amazon')
plt.plot(HK00700Df['Close'],color='red',label='tecent')
plt.title('Comparison of GOOG and AMZN and tecent')
plt.xlabel('Date')
plt.ylabel('Closing price')
plt.legend()
plt.grid(True)
plt.show()
plt.figure(figsize=(10,5))#宽度和高度的单位为英寸
plt.plot(GOOGDf['Volume'],color='blue',label='google')
plt.plot(AMZNDf['Volume'],color='Green',label='amazon')
plt.plot(HK00700Df['Volume'],color='red',label='tencent')
plt.title('Comparison of GOOG and AMZN and tencent')
plt.xlabel('Date')
plt.ylabel('Volume')
plt.legend()
plt.grid(True)
plt.show()
plt.figure(figsize=(10,5))#宽度和高度的单位为英寸
plt.plot(FBDf['Close'],color='blue',label='facebook')
plt.plot(AAPLDf['Close'],color='Green',label='apple')
plt.plot(BABADf['Close'],color='red',label='alibaba')
plt.title('Comparison of facebook and apple and alibaba')
plt.xlabel('Date')
plt.ylabel('Closing price')
plt.legend()
plt.grid(True)
plt.show()
GOOGDf.info()
AMZNDf.info()
FBDf.info()
BABADf.info()
HK00700Df.info()
AAPLDf.info()
DatetimeIndex: 397 entries, 2017-01-03 to 2018-07-31
Data columns (total 6 columns):
Open 397 non-null float64
High 397 non-null float64
Low 397 non-null float64
Close 397 non-null float64
Adj Close 397 non-null float64
Volume 397 non-null int32
dtypes: float64(5), int32(1)
memory usage: 20.2 KB
DatetimeIndex: 397 entries, 2017-01-03 to 2018-07-31
Data columns (total 6 columns):
Open 397 non-null float64
High 397 non-null float64
Low 397 non-null float64
Close 397 non-null float64
Adj Close 397 non-null float64
Volume 397 non-null int32
dtypes: float64(5), int32(1)
memory usage: 20.2 KB
DatetimeIndex: 397 entries, 2017-01-03 to 2018-07-31
Data columns (total 6 columns):
Open 397 non-null float64
High 397 non-null float64
Low 397 non-null float64
Close 397 non-null float64
Adj Close 397 non-null float64
Volume 397 non-null int32
dtypes: float64(5), int32(1)
memory usage: 20.2 KB
DatetimeIndex: 397 entries, 2017-01-03 to 2018-07-31
Data columns (total 6 columns):
Open 397 non-null float64
High 397 non-null float64
Low 397 non-null float64
Close 397 non-null float64
Adj Close 397 non-null float64
Volume 397 non-null int32
dtypes: float64(5), int32(1)
memory usage: 20.2 KB
DatetimeIndex: 390 entries, 2017-01-03 to 2018-08-01
Data columns (total 6 columns):
Open 390 non-null float64
High 390 non-null float64
Low 390 non-null float64
Close 390 non-null float64
Adj Close 390 non-null float64
Volume 390 non-null int32
dtypes: float64(5), int32(1)
memory usage: 19.8 KB
DatetimeIndex: 397 entries, 2017-01-03 to 2018-07-31
Data columns (total 6 columns):
Open 397 non-null float64
High 397 non-null float64
Low 397 non-null float64
Close 397 non-null float64
Adj Close 397 non-null float64
Volume 397 non-null int32
dtypes: float64(5), int32(1)
memory usage: 20.2 KB
def change(column):
buyPrice = column[0]
sellPrice = column[397-1]
priceChange = (sellPrice-buyPrice)/buyPrice
if(priceChange>0):
print('股票累计上涨=',priceChange)
elif(priceChange<0):
print('股票累计下跌=',priceChange)
else:
print('股票累计没有变化=',priceChange)
return priceChange
GOOG_closeCol = GOOGDf['Close']
GOOGChange = change(GOOG_closeCol)
股票累计上涨= 0.5484010313353657
AAPL_closeCol = AAPLDf['Close']
AAPLChange = change(AAPL_closeCol)
股票累计上涨= 0.6383124384276806
FB_closeCol = FBDf['Close']
FBChange = change(FB_closeCol)
股票累计上涨= 0.4768098624267512
AMZN_closeCol = AMZNDf['Close']
AMZNChange = change(AMZN_closeCol)
股票累计上涨= 1.358379637099067
BABA_closeCol = BABADf['Close']
BABAChange = change(BABA_closeCol)
股票累计上涨= 1.1132054201626507
HK00700_closeCol = HK00700Df['Close']
HK00700Change = change(HK00700_closeCol)#会报错,因为行数比其他数据帧少7行
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_value(self, series, key)
3102 return self._engine.get_value(s, k,
-> 3103 tz=getattr(series.dtype, 'tz', None))
3104 except KeyError as e1:
IndexError: index out of bounds
#报错超出边界,从HK00700Df.info()也能看出,腾讯的数据比其他数据少了7条
HK00700_Price = HK00700Df['Close']
HK00700_buyPrice = HK00700_Price[0]
HK00700_sellPrice = HK00700_Price[(len(HK00700_Price)-1)]
HK00700_change = (HK00700_sellPrice-HK00700_buyPrice)/HK00700_buyPrice
print('股票累计上涨=',HK00700_change)
股票累计上涨= 0.8743400804965179
plt.figure(figsize=(15,10))#宽度和高度的单位为英寸
plt.plot(FBDf['Close'],color='blue',label='facebook')
plt.plot(AAPLDf['Close'],color='Green',label='apple')
plt.plot(BABADf['Close'],color='red',label='alibaba')
plt.plot(GOOGDf['Close'],color='black',label='google')
plt.plot(AMZNDf['Close'],color='orange',label='amazon')
plt.plot(HK00700Df['Close'],color='yellow',label='tencent')
plt.title('Comparison of facebook and apple and alibaba and google and amazon and tencent')
plt.xlabel('Date')
plt.ylabel('Closing price')
plt.legend()
plt.grid(True)
plt.show()
writer = pd.ExcelWriter('gafata.xls')
GOOGDf.to_excel(writer,'google')
AAPLDf.to_excel(writer,'apple')
FBDf.to_excel(writer,'facebook')
AMZNDf.to_excel(writer,'amazon')
BABADf.to_excel(writer,'alibaba')
HK00700Df.to_excel(writer,'tencent')
writer.save()
python index out of bounds_使用python中遇到的坑相关推荐
- Python 爬虫中国知网论文过程中遇到的坑及解决办法
假期,老师给布置了 Python 爬虫中国知网论文的任务,目前实现了登录和搜索功能,先写一下遇到的坑和解决办法吧. Python 爬虫中国知网论文过程中遇到的坑及解决办法 一. selenium 模块 ...
- python index out of bounds_错误:索引2超出大小为1的轴0的界限
我对下面用Python(odeint)求解激光速率方程(一阶常微分方程)的小程序有问题.在 当我运行程序时,总是会出现一个错误:index 2 is out of bounds for axis 0 ...
- 对Python参数类型详解以及学习中遇到的坑
由于之前遇到过几次有关于参数类型的坑,以及经常容易把一些参数类型搞混淆,现在做一下有关参数类型的总结记录以及对之前踩坑经历的分析. 参数类型 首先我们列举一下有关于Python的参数类型,以及实际上的 ...
- python打包exe 之打包sklearn模型中的各种坑及其解决方法。
之前学习了如何打包,如何建立虚拟环境打包,以及如何带资源打包exe. python打包成exe 可执行文件 .教程 使用pipenv建立虚拟环境解决python打包exe文件过大的问题(附打包带图标, ...
- python index函数时间复杂度_初学python之以时间复杂度去理解列表常见使用方法
列表list,一个有序的队列 列表内的个体为元素,由若干个元素按照顺序进行排列,列表是可变化的,也就是说可以增删 list定义 常用的列表定义方式: 使用[] 或者 a = list() 取数列表可以 ...
- split函数python 未定义_Python字符串方法split()中的一道坑
初看这个方法还挺好用的,用来切割字符串真是的是非常方便,返回的字符串数组也非常容易处理. 于是乎看了一眼就应用到我的程序当中去了. 用来切割如下形式的字符串 s = 'Jul 24 21:38:25 ...
- 公众号python训练营真的假的_python中的这些坑,早看早避免。
python中的这些坑,早看早避免. 说一说python中遇到的坑,躲坑看这一篇就够了 传递参数时候不要使用列表 def foo(num,age=[]): age.append(num) print( ...
- python处理表格数据教程_用Python的pandas框架操作Excel文件中的数据教程
引言 本文的目的,是向您展示如何使用pandas来执行一些常见的Excel任务.有些例子比较琐碎,但我觉得展示这些简单的东西与那些你可以在其他地方找到的复杂功能同等重要.作为额外的福利,我将会进行一些 ...
- python把数据写入excel_Python向excel中写入数据的方法
Python向excel中写入数据的方法 最近做了一项工作需要把处理的数据写入到Excel表格中进行保存,所以在此就简单介绍使用Python如何把数据保存到excel表格中. 数据导入之前需要安装 x ...
最新文章
- java signature 性能_Java常见bean mapper的性能及原理分析
- Postgresql: 时间戳long,TimeStamp,Date,String互转
- 大工计算机基础在线作业答案,大工11春《计算机文化基础》在线作业及答案(国外英文资料).doc...
- 深入浅出Android App耗电量统计
- 2017寒假第一篇随笔(寒假作业一)
- BUUCTF [FlareOn1]Bob Doge [GXYCTF2019]
- .Net Core小技巧 - Swagger适配虚拟目录及二级目录
- datasg中数据的存储结构
- ElasticSearch---------------------step2,了解elasticsearch相关的基本概念
- 转载爱哥自定义View系列--Paint详解
- linux c select函数返回值,linux c中select使用技巧
- python下批量修改图片格式和大小
- MATLAB---CAD绘制Bezier曲线算法
- 热切换Log4j日志级别
- 新浪云python示例_新浪云的基本配置
- python pandas向已有excel添加新表sheet/添加数据
- 使用阿尔卑斯山法进行高效时间管理
- apple API常用英语名词
- SSH——Hibernate初学者之旅(五)
- 思考总结:领域知识图谱平台构建与业务应用
热门文章
- 信息系统开发与管理第一遍总结
- (load和initialize)不要被你的log迷惑了你对问题的判断
- matlab 2013至2016 32bit、64bit破解版集合 百度云盘下载
- 第三天 引用类型选择结构循环结构【悟空教程】
- 北理计算机优营会被鸽吗,被放鸽子以后~
- html5快速制作,html5动画制作(教你如何快速绘制HTML5动画)
- c语言实现图书借阅系统
- C++初阶习题(牛客)【4】Fibonacci数列
- canvas画圆又毛边
- 直播报名|美团技术沙龙56期:美团计算机视觉与多媒体技术实践--ACM MM2020专场...