GAFATA股票数据分析

1import pandas as pd 
2import matplotlib.pyplot as plt
1gupiao={'谷歌':'GOOG','亚马逊':'AMZN','Facebook':'FB',
2            '苹果':'AAPL','阿里巴巴':'BABA','腾讯':'0700.hk'}
1from pandas_datareader import data

阿里巴巴

1start_date = '2017-01-01'
2end_date = '2018-01-01'
3ali=data.get_data_yahoo(gupiao["阿里巴巴"],start_date,end_date)
1ali.head()
             High        Low       Open      Close    Volume  Adj Close

Date
2017-01-03 89.000000 88.080002 89.000000 88.599998 8789400 88.599998 2017-01-04 90.889999 88.580002 88.985001 90.510002 11490200 90.510002 2017-01-05 94.809998 91.639999 91.910004 94.370003 16821500 94.370003 2017-01-06 94.500000 93.000000 94.400002 93.889999 7639800 93.889999 2017-01-09 95.650002 93.309998 94.160004 94.720001 10792900 94.720001

1def  change(column):
2    prichange=(column[column.size-1]-column[0])/column[0]
3    if (prichange > 0):
4        print("股票累计上涨:",prichange*100,"%")
5    elif (prichange == 0):
6        print("股票基本不变:",prichange*100,"%")
7    else :
8        print("股票累计下跌:",prichange*100,"%")
9    return prichange
1alichange=change(ali["Low"])

股票累计上涨: 94.36874817125808 %

1yamaxun=data.get_data_yahoo(gupiao["亚马逊"],start_date,end_date)
2facebook=data.get_data_yahoo(gupiao["Facebook"],start_date,end_date)
3apple=data.get_data_yahoo(gupiao["苹果"],start_date,end_date)
4guge=data.get_data_yahoo(gupiao["谷歌"],start_date,end_date)
5tengxun=data.get_data_yahoo(gupiao["腾讯"],start_date,end_date)
1yamaxunchange=change(yamaxun["Low"])

股票累计上涨: 56.1455103569973 %

1facechange=change(facebook["Low"])

股票累计上涨: 52.76599727333859 %

1applechange=change(apple["Low"])

股票累计上涨: 47.45555774722253 %

1gugechange=change(guge["Low"])

股票累计上涨: 34.686780208213435 %

1tengxunchange=change(tengxun["Low"])

股票累计上涨: 116.80851388484874 %

1ali.info()

<class 'pandas.core.frame.DataFrame'> DatetimeIndex: 251 entries, 2017-01-03 to 2017-12-29 Data columns (total 6 columns): High 251 non-null float64 Low 251 non-null float64 Open 251 non-null float64 Close 251 non-null float64 Volume 251 non-null int64 Adj Close 251 non-null float64 dtypes: float64(5), int64(1) memory usage: 23.7 KB

1ali.dtypes

High float64 Low float64 Open float64 Close float64 Volume int64 Adj Close float64 dtype: object

1ali.describe()
         High         Low        Open       Close        Volume  \

count 251.000000 251.000000 251.000000 251.000000 2.510000e+02
mean 143.203490 140.260857 141.909542 141.791793 1.550723e+07
std 32.339500 31.505186 32.090767 31.875025 9.377487e+06
min 89.000000 88.080002 88.985001 88.599998 4.120700e+06
25% 109.124500 107.489998 108.430000 108.169998 9.129950e+06
50% 144.380005 141.479996 143.000000 143.009995 1.341080e+07
75% 175.139999 171.584999 173.595001 173.250000 1.864455e+07
max 191.750000 189.369995 191.539993 191.190002 8.093690e+07

    Adj Close  

count 251.000000
mean 141.791793
std 31.875025
min 88.599998
25% 108.169998
50% 143.009995
75% 173.250000
max 191.190002

1ali.plot(y="Close")
2plt.xlabel("时间")
3plt.ylabel("股价(美元)")
4plt.title("2017年阿里巴巴股价走势")
5plt.grid(True)
6plt.show()

Image

1ali2=ali.reset_index(drop=False)
1ali2.plot(x="Volume",y="Close",kind="scatter")
2plt.grid(True)
3plt.show()

Image

1ali.corr()
           High       Low      Open     Close    Volume  Adj Close

High 1.000000 0.998782 0.999281 0.999077 0.432467 0.999077 Low 0.998782 1.000000 0.998798 0.999249 0.401456 0.999249 Open 0.999281 0.998798 1.000000 0.998226 0.424686 0.998226 Close 0.999077 0.999249 0.998226 1.000000 0.415801 1.000000 Volume 0.432467 0.401456 0.424686 0.415801 1.000000 0.415801 Adj Close 0.999077 0.999249 0.998226 1.000000 0.415801 1.000000

1type(ali.index)

pandas.core.indexes.datetimes.DatetimeIndex

1a=ali.index
1type(a)

pandas.core.indexes.datetimes.DatetimeIndex

1tengxun["Close_dollar"]=tengxun["Close"]*0.1278
1tengxun.head()
              High         Low        Open       Close      Volume  \

Date
2017-01-03 191.100006 188.000000 188.000000 189.399994 9637272.0
2017-01-04 190.500000 188.100006 190.399994 189.000000 11411490.0
2017-01-05 194.199997 190.699997 191.000000 193.300003 20543005.0
2017-01-06 196.800003 194.699997 196.199997 195.100006 20077760.0
2017-01-09 196.899994 195.199997 196.699997 195.600006 13605277.0

         Adj Close  Close_dollar  

Date
2017-01-03 188.557770 24.205319
2017-01-04 188.159561 24.154200
2017-01-05 192.440445 24.703740
2017-01-06 194.232437 24.933781
2017-01-09 194.730209 24.997681

 1ax1=ali.plot(y="Close",label="阿里巴巴")
 2yamaxun.plot(ax=ax1,y="Close",label="亚马逊")
 3apple.plot(ax=ax1,y="Close",label="苹果")
 4guge.plot(ax=ax1,y="Close",label="谷歌")
 5tengxun.plot(ax=ax1,y="Close_dollar",label="腾讯")
 6facebook.plot(ax=ax1,y="Close",label="Facebook")
 7plt.xlabel("时间")
 8plt.ylabel("股价(美元)")
 9plt.grid(True)
10plt.show()

Image

1ax1=yamaxun.plot(y="Close",label="亚马逊")
2guge.plot(ax=ax1,y="Close",label="谷歌")
3plt.xlabel("时间")
4plt.ylabel("股价(美元)")
5plt.grid(True)
6plt.show()

Image

1ax2=ali.plot(y="Close",label="阿里巴巴")
2apple.plot(ax=ax2,y="Close",label="苹果")
3tengxun.plot(ax=ax2,y="Close_dollar",label="腾讯")
4facebook.plot(ax=ax2,y="Close",label="Facebook")
5plt.xlabel("时间")
6plt.ylabel("股价(美元)")
7plt.grid(True)
8plt.show()

Image

1gpmean=[ali["Close"].mean(),apple["Close"].mean(),guge["Close"].mean(),facebook["Close"].mean(),yamaxun["Close"].mean(),tengxun["Close_dollar"].mean()]
1gpmean

[141.7917926074024, 150.55107548322334, 921.7808373439834, 156.57617537053932, 968.1670116409363, 37.2805998528634]

1gps=pd.Series(gpmean,index=["阿里巴巴","苹果","谷歌","Facebook","亚马逊","腾讯"])
1gps

阿里巴巴 141.791793 苹果 150.551075 谷歌 921.780837 Facebook 156.576175 亚马逊 968.167012 腾讯 37.280600 dtype: float64

1gps.plot(kind="bar",label="GAFATA")
2plt.xlabel("公司名称")
3plt.ylabel("股价(美元)")
4plt.grid(True)
5plt.show()

Image

1closedf=pd.DataFrame()
2closedf=pd.concat([closedf,ali["Close"],apple["Close"],guge["Close"],facebook["Close"],yamaxun["Close"],tengxun["Close_dollar"]],axis=1)
1closedf.head()
             阿里巴巴          苹果          谷歌    Facebook         亚马逊  \

Date
2017-01-03 88.599998 116.150002 786.140015 116.860001 753.669983
2017-01-04 90.510002 116.019997 786.900024 118.690002 757.179993
2017-01-05 94.370003 116.610001 794.020020 120.669998 780.450012
2017-01-06 93.889999 117.910004 806.150024 123.410004 795.989990
2017-01-09 94.720001 118.989998 806.650024 124.900002 796.919983

               腾讯  

Date
2017-01-03 24.205319
2017-01-04 24.154200
2017-01-05 24.703740
2017-01-06 24.933781
2017-01-09 24.997681

1closedf.columns=["阿里巴巴","苹果","谷歌","Facebook","亚马逊","腾讯"]#直接命名
1closedf.plot(kind="box")
2plt.xlabel("公司名称")
3plt.ylabel("股价(美元)")
4plt.grid(True)
5plt.show()

Image