概述
这周开始学习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)
~Anaconda3libsite-packagespandascoreindexesbase.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 index out of bounds_使用python中遇到的坑所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复