要知道,NumPy的常用数学和统计分析的函数非常多,如果我们一个一个的分散来讲,一来非常枯燥,二来呢也记不住,就仿佛又回到了昏昏欲睡的课堂,今天我们用一个背景例子来串联一下这些零散的知识点。

我们通过分析苹果公司的股票价格,来串讲NumPy的常用函数用法

我们在我们python文件的同级目录下放置数据文件AAPL.csv,用excel文件可以打开看看里面是什么样的:

import numpy as np

import pandas as pd

from pandas import Series,DataFrame
app = pd.read_csv('./AAPL.csv')

app.shape
(9814, 7)
app.head()
Date Open High Low Close Adj Close Volume
0 1980-12-12 0.513393 0.515625 0.513393 0.513393 0.407747 117258400.0
1 1980-12-15 0.488839 0.488839 0.486607 0.486607 0.386473 43971200.0
2 1980-12-16 0.453125 0.453125 0.450893 0.450893 0.358108 26432000.0
3 1980-12-17 0.462054 0.464286 0.462054 0.462054 0.366972 21610400.0
4 1980-12-18 0.475446 0.477679 0.475446 0.475446 0.377609 18362400.0
app.tail()
Date Open High Low Close Adj Close Volume
9809 2019-11-06 256.769989 257.489990 255.369995 257.239990 256.470001 18966100.0
9810 2019-11-07 258.739990 260.350006 258.109985 259.429993 259.429993 23735100.0
9811 2019-11-08 258.690002 260.440002 256.850006 260.140015 260.140015 17496600.0
9812 2019-11-11 258.299988 262.470001 258.279999 262.200012 262.200012 20455300.0
9813 2019-11-12 261.549988 262.790009 260.920013 261.959991 261.959991 21826100.0
app.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 9814 entries, 0 to 9813
Data columns (total 7 columns):
Date         9814 non-null object
Open         9813 non-null float64
High         9813 non-null float64
Low          9813 non-null float64
Close        9813 non-null float64
Adj Close    9813 non-null float64
Volume       9813 non-null float64
dtypes: float64(6), object(1)
memory usage: 536.8+ KB
app[app.isnull().any(axis = 1)]
Date Open High Low Close Adj Close Volume
165 1981-08-10 NaN NaN NaN NaN NaN NaN
app.dropna(inplace = True) 
app.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 9813 entries, 0 to 9813
Data columns (total 7 columns):
Date         9813 non-null object
Open         9813 non-null float64
High         9813 non-null float64
Low          9813 non-null float64
Close        9813 non-null float64
Adj Close    9813 non-null float64
Volume       9813 non-null float64
dtypes: float64(6), object(1)
memory usage: 613.3+ KB
app['Date'] = pd.to_datetime(app['Date']) 
app.dtypes
Date         datetime64[ns]
Open                float64
High                float64
Low                 float64
Close               float64
Adj Close           float64
Volume              float64
dtype: object
app.head()
Date Open High Low Close Adj Close Volume
0 1980-12-12 0.513393 0.515625 0.513393 0.513393 0.407747 117258400.0
1 1980-12-15 0.488839 0.488839 0.486607 0.486607 0.386473 43971200.0
2 1980-12-16 0.453125 0.453125 0.450893 0.450893 0.358108 26432000.0
3 1980-12-17 0.462054 0.464286 0.462054 0.462054 0.366972 21610400.0
4 1980-12-18 0.475446 0.477679 0.475446 0.475446 0.377609 18362400.0
app2 = app.set_index('Date')
app2
Open High Low Close Adj Close Volume
Date
1980-12-12 0.513393 0.515625 0.513393 0.513393 0.407747 117258400.0
1980-12-15 0.488839 0.488839 0.486607 0.486607 0.386473 43971200.0
1980-12-16 0.453125 0.453125 0.450893 0.450893 0.358108 26432000.0
1980-12-17 0.462054 0.464286 0.462054 0.462054 0.366972 21610400.0
1980-12-18 0.475446 0.477679 0.475446 0.475446 0.377609 18362400.0
... ... ... ... ... ... ...
2019-11-06 256.769989 257.489990 255.369995 257.239990 256.470001 18966100.0
2019-11-07 258.739990 260.350006 258.109985 259.429993 259.429993 23735100.0
2019-11-08 258.690002 260.440002 256.850006 260.140015 260.140015 17496600.0
2019-11-11 258.299988 262.470001 258.279999 262.200012 262.200012 20455300.0
2019-11-12 261.549988 262.790009 260.920013 261.959991 261.959991 21826100.0

9813 rows × 6 columns

app2['Adj Close'].plot(kind ='line')

在这里插入图片描述

app2.resample('M').median().plot(kind ='line')

在这里插入图片描述

app2.resample('M').median()[['Adj Close','Open']].plot(kind ='line')

在这里插入图片描述

app2.resample('Y').median()[['High','Low']].plot(kind ='line')

在这里插入图片描述 在这里插入图片描述

版权声明:如无特殊说明,文章均为本站原创,转载请注明出处

本文链接:http://wakemeupnow.cn/article/apple/