# 导入 pandas 模块
import pandas as pd
#pandas是解决数据分析
# 设置直接显示图片
%matplotlib inline
# %为魔法命令。matplotlib为最著名的Python图表绘制扩展库
stock = pd.read_csv('stock.csv', parse_dates=True, index_col='Date')
stock
#运行正常。显示出数据。它就是读取的stock.csv这个文件
|
Open |
High |
Low |
Close |
Volume |
---|
Date |
|
|
|
|
|
---|
2017-06-01 |
153.17 |
153.33 |
152.22 |
153.18 |
16404088 |
---|
2017-06-02 |
153.58 |
155.45 |
152.89 |
155.45 |
27770715 |
---|
2017-06-05 |
154.34 |
154.45 |
153.46 |
153.93 |
25331662 |
---|
2017-06-06 |
153.90 |
155.81 |
153.78 |
154.45 |
26624926 |
---|
2017-06-07 |
155.02 |
155.98 |
154.48 |
155.37 |
21069647 |
---|
2017-06-08 |
155.25 |
155.54 |
154.40 |
154.99 |
21250798 |
---|
2017-06-09 |
155.19 |
155.19 |
146.02 |
148.98 |
64882657 |
---|
2017-06-12 |
145.74 |
146.09 |
142.51 |
145.42 |
72307330 |
---|
2017-06-13 |
147.16 |
147.45 |
145.15 |
146.59 |
34165445 |
---|
2017-06-14 |
147.50 |
147.50 |
143.84 |
145.16 |
31531232 |
---|
2017-06-15 |
143.32 |
144.48 |
142.21 |
144.29 |
32165373 |
---|
2017-06-16 |
143.78 |
144.50 |
142.20 |
142.27 |
50361093 |
---|
2017-06-19 |
143.66 |
146.74 |
143.66 |
146.34 |
32541404 |
---|
2017-06-20 |
146.87 |
146.87 |
144.94 |
145.01 |
24900073 |
---|
2017-06-21 |
145.52 |
146.07 |
144.61 |
145.87 |
21265751 |
---|
2017-06-22 |
145.77 |
146.70 |
145.12 |
145.63 |
19106294 |
---|
2017-06-23 |
145.13 |
147.16 |
145.11 |
146.28 |
35439389 |
---|
2017-06-26 |
147.17 |
148.28 |
145.38 |
145.82 |
25692361 |
---|
2017-06-27 |
145.01 |
146.16 |
143.62 |
143.73 |
24761891 |
---|
2017-06-28 |
144.49 |
146.11 |
143.16 |
145.83 |
22082432 |
---|
2017-06-29 |
144.71 |
145.13 |
142.28 |
143.68 |
31499368 |
---|
2017-06-30 |
144.45 |
144.96 |
143.78 |
144.02 |
23024107 |
---|
stock.head()
#head是显示头部数据,默认是前五个数据
|
Open |
High |
Low |
Close |
Volume |
---|
Date |
|
|
|
|
|
---|
2017-06-01 |
153.17 |
153.33 |
152.22 |
153.18 |
16404088 |
---|
2017-06-02 |
153.58 |
155.45 |
152.89 |
155.45 |
27770715 |
---|
2017-06-05 |
154.34 |
154.45 |
153.46 |
153.93 |
25331662 |
---|
2017-06-06 |
153.90 |
155.81 |
153.78 |
154.45 |
26624926 |
---|
2017-06-07 |
155.02 |
155.98 |
154.48 |
155.37 |
21069647 |
---|
stock.head(3)
#()里面可以设置你需要查找的数量,比如我需要查找前3个的数据,只需要在()里输入3就可以了
|
Open |
High |
Low |
Close |
Volume |
---|
Date |
|
|
|
|
|
---|
2017-06-01 |
153.17 |
153.33 |
152.22 |
153.18 |
16404088 |
---|
2017-06-02 |
153.58 |
155.45 |
152.89 |
155.45 |
27770715 |
---|
2017-06-05 |
154.34 |
154.45 |
153.46 |
153.93 |
25331662 |
---|
stock.tail()
#tail是显示尾部的数据,默认就是最后5个数据。()也是同上面的一样,输入数字,代表是查找对应最后几个的数据。
|
Open |
High |
Low |
Close |
Volume |
---|
Date |
|
|
|
|
|
---|
2017-06-26 |
147.17 |
148.28 |
145.38 |
145.82 |
25692361 |
---|
2017-06-27 |
145.01 |
146.16 |
143.62 |
143.73 |
24761891 |
---|
2017-06-28 |
144.49 |
146.11 |
143.16 |
145.83 |
22082432 |
---|
2017-06-29 |
144.71 |
145.13 |
142.28 |
143.68 |
31499368 |
---|
2017-06-30 |
144.45 |
144.96 |
143.78 |
144.02 |
23024107 |
---|
stock.shape
#shape表示数据的形状,行和列。本数据就是22行,5列
(22, 5)
stock.columns
#我的理解就是显示文件列的数据标题。
Index(['Open', 'High', 'Low', 'Close', 'Volume'], dtype='object')
stock.index
#我的理解就是显示数据行的标题
DatetimeIndex(['2017-06-01', '2017-06-02', '2017-06-05', '2017-06-06',
'2017-06-07', '2017-06-08', '2017-06-09', '2017-06-12',
'2017-06-13', '2017-06-14', '2017-06-15', '2017-06-16',
'2017-06-19', '2017-06-20', '2017-06-21', '2017-06-22',
'2017-06-23', '2017-06-26', '2017-06-27', '2017-06-28',
'2017-06-29', '2017-06-30'],
dtype='datetime64[ns]', name='Date', freq=None)
stock.info()
#我的理解就是像查看文件属性是一样的
DatetimeIndex: 22 entries, 2017-06-01 to 2017-06-30
Data columns (total 5 columns):
Open 22 non-null float64
High 22 non-null float64
Low 22 non-null float64
Close 22 non-null float64
Volume 22 non-null int64
dtypes: float64(4), int64(1)
memory usage: 1.0 KB
stock.info(10)
#后面的()有何意义呢?
DatetimeIndex: 22 entries, 2017-06-01 to 2017-06-30
Data columns (total 5 columns):
Open 22 non-null float64
High 22 non-null float64
Low 22 non-null float64
Close 22 non-null float64
Volume 22 non-null int64
dtypes: float64(4), int64(1)
memory usage: 1.0 KB
stock.describe()
|
Open |
High |
Low |
Close |
Volume |
---|
count |
22.000000 |
22.000000 |
22.000000 |
22.000000 |
2.200000e+01 |
---|
mean |
148.215000 |
149.088636 |
146.582727 |
147.831364 |
3.109900e+07 |
---|
std |
4.450871 |
4.337027 |
4.507623 |
4.434315 |
1.416738e+07 |
---|
min |
143.320000 |
144.480000 |
142.200000 |
142.270000 |
1.640409e+07 |
---|
25% |
144.785000 |
146.095000 |
143.630000 |
145.047500 |
2.231785e+07 |
---|
50% |
146.320000 |
147.015000 |
145.025000 |
145.850000 |
2.615864e+07 |
---|
75% |
153.477500 |
154.170000 |
150.670000 |
152.130000 |
3.244740e+07 |
---|
max |
155.250000 |
155.980000 |
154.480000 |
155.450000 |
7.230733e+07 |
---|
stock.plot()
#绘图,折线图
stock.plot(y='Open')
#()里设置要显示的折线图数据
stock['Close']
#使用[]可以进行数据的索引。搜索某一项的数据
Date
2017-06-01 153.18
2017-06-02 155.45
2017-06-05 153.93
2017-06-06 154.45
2017-06-07 155.37
2017-06-08 154.99
2017-06-09 148.98
2017-06-12 145.42
2017-06-13 146.59
2017-06-14 145.16
2017-06-15 144.29
2017-06-16 142.27
2017-06-19 146.34
2017-06-20 145.01
2017-06-21 145.87
2017-06-22 145.63
2017-06-23 146.28
2017-06-26 145.82
2017-06-27 143.73
2017-06-28 145.83
2017-06-29 143.68
2017-06-30 144.02
Name: Close, dtype: float64
stock['Open']
Date
2017-06-01 153.17
2017-06-02 153.58
2017-06-05 154.34
2017-06-06 153.90
2017-06-07 155.02
2017-06-08 155.25
2017-06-09 155.19
2017-06-12 145.74
2017-06-13 147.16
2017-06-14 147.50
2017-06-15 143.32
2017-06-16 143.78
2017-06-19 143.66
2017-06-20 146.87
2017-06-21 145.52
2017-06-22 145.77
2017-06-23 145.13
2017-06-26 147.17
2017-06-27 145.01
2017-06-28 144.49
2017-06-29 144.71
2017-06-30 144.45
Name: Open, dtype: float64
stock.Close
#不使用中括号也可以索引
Date
2017-06-01 153.18
2017-06-02 155.45
2017-06-05 153.93
2017-06-06 154.45
2017-06-07 155.37
2017-06-08 154.99
2017-06-09 148.98
2017-06-12 145.42
2017-06-13 146.59
2017-06-14 145.16
2017-06-15 144.29
2017-06-16 142.27
2017-06-19 146.34
2017-06-20 145.01
2017-06-21 145.87
2017-06-22 145.63
2017-06-23 146.28
2017-06-26 145.82
2017-06-27 143.73
2017-06-28 145.83
2017-06-29 143.68
2017-06-30 144.02
Name: Close, dtype: float64
stock['Close']['2017-06-01']
#可以精准索引到某一项
153.18000000000001
stock['Close']['2017-06-07']
155.37
stock['Close'][0]
153.18000000000001
stock['Close'][5]
154.99000000000001
stock['Close'][2]
#我的理解是后面的中括号里的数字是表示该数字的个数的下一个数的内容
153.93000000000001
stock[['Close']]
#这个跟前面的stock.['Open']差不多,只是显示出来不一样
|
Close |
---|
Date |
|
---|
2017-06-01 |
153.18 |
---|
2017-06-02 |
155.45 |
---|
2017-06-05 |
153.93 |
---|
2017-06-06 |
154.45 |
---|
2017-06-07 |
155.37 |
---|
2017-06-08 |
154.99 |
---|
2017-06-09 |
148.98 |
---|
2017-06-12 |
145.42 |
---|
2017-06-13 |
146.59 |
---|
2017-06-14 |
145.16 |
---|
2017-06-15 |
144.29 |
---|
2017-06-16 |
142.27 |
---|
2017-06-19 |
146.34 |
---|
2017-06-20 |
145.01 |
---|
2017-06-21 |
145.87 |
---|
2017-06-22 |
145.63 |
---|
2017-06-23 |
146.28 |
---|
2017-06-26 |
145.82 |
---|
2017-06-27 |
143.73 |
---|
2017-06-28 |
145.83 |
---|
2017-06-29 |
143.68 |
---|
2017-06-30 |
144.02 |
---|
stock.Close
Date
2017-06-01 153.18
2017-06-02 155.45
2017-06-05 153.93
2017-06-06 154.45
2017-06-07 155.37
2017-06-08 154.99
2017-06-09 148.98
2017-06-12 145.42
2017-06-13 146.59
2017-06-14 145.16
2017-06-15 144.29
2017-06-16 142.27
2017-06-19 146.34
2017-06-20 145.01
2017-06-21 145.87
2017-06-22 145.63
2017-06-23 146.28
2017-06-26 145.82
2017-06-27 143.73
2017-06-28 145.83
2017-06-29 143.68
2017-06-30 144.02
Name: Close, dtype: float64
stock[['Open','Close']]
#搜索Open和Close这两项
|
Open |
Close |
---|
Date |
|
|
---|
2017-06-01 |
153.17 |
153.18 |
---|
2017-06-02 |
153.58 |
155.45 |
---|
2017-06-05 |
154.34 |
153.93 |
---|
2017-06-06 |
153.90 |
154.45 |
---|
2017-06-07 |
155.02 |
155.37 |
---|
2017-06-08 |
155.25 |
154.99 |
---|
2017-06-09 |
155.19 |
148.98 |
---|
2017-06-12 |
145.74 |
145.42 |
---|
2017-06-13 |
147.16 |
146.59 |
---|
2017-06-14 |
147.50 |
145.16 |
---|
2017-06-15 |
143.32 |
144.29 |
---|
2017-06-16 |
143.78 |
142.27 |
---|
2017-06-19 |
143.66 |
146.34 |
---|
2017-06-20 |
146.87 |
145.01 |
---|
2017-06-21 |
145.52 |
145.87 |
---|
2017-06-22 |
145.77 |
145.63 |
---|
2017-06-23 |
145.13 |
146.28 |
---|
2017-06-26 |
147.17 |
145.82 |
---|
2017-06-27 |
145.01 |
143.73 |
---|
2017-06-28 |
144.49 |
145.83 |
---|
2017-06-29 |
144.71 |
143.68 |
---|
2017-06-30 |
144.45 |
144.02 |
---|
stock.loc['2017-06-01', 'Close']
153.18000000000001
stock.loc[:, 'Close']
Date
2017-06-01 153.18
2017-06-02 155.45
2017-06-05 153.93
2017-06-06 154.45
2017-06-07 155.37
2017-06-08 154.99
2017-06-09 148.98
2017-06-12 145.42
2017-06-13 146.59
2017-06-14 145.16
2017-06-15 144.29
2017-06-16 142.27
2017-06-19 146.34
2017-06-20 145.01
2017-06-21 145.87
2017-06-22 145.63
2017-06-23 146.28
2017-06-26 145.82
2017-06-27 143.73
2017-06-28 145.83
2017-06-29 143.68
2017-06-30 144.02
Name: Close, dtype: float64
stock.loc[:,'Open']
#跟上面使用中括号是一样的。
Date
2017-06-01 153.17
2017-06-02 153.58
2017-06-05 154.34
2017-06-06 153.90
2017-06-07 155.02
2017-06-08 155.25
2017-06-09 155.19
2017-06-12 145.74
2017-06-13 147.16
2017-06-14 147.50
2017-06-15 143.32
2017-06-16 143.78
2017-06-19 143.66
2017-06-20 146.87
2017-06-21 145.52
2017-06-22 145.77
2017-06-23 145.13
2017-06-26 147.17
2017-06-27 145.01
2017-06-28 144.49
2017-06-29 144.71
2017-06-30 144.45
Name: Open, dtype: float64
stock.loc['2017-06-01':'2017-06-05', 'Close']
#可以搜索某一个时间段的数据
Date
2017-06-01 153.18
2017-06-02 155.45
2017-06-05 153.93
Name: Close, dtype: float64
stock.loc['2017-06-06':'2017-06-21', 'Close']
Date
2017-06-06 154.45
2017-06-07 155.37
2017-06-08 154.99
2017-06-09 148.98
2017-06-12 145.42
2017-06-13 146.59
2017-06-14 145.16
2017-06-15 144.29
2017-06-16 142.27
2017-06-19 146.34
2017-06-20 145.01
2017-06-21 145.87
Name: Close, dtype: float64
stock.loc['2017-06-01':'2017-06-05', 'Open':'Close']
|
Open |
High |
Low |
Close |
---|
Date |
|
|
|
|
---|
2017-06-01 |
153.17 |
153.33 |
152.22 |
153.18 |
---|
2017-06-02 |
153.58 |
155.45 |
152.89 |
155.45 |
---|
2017-06-05 |
154.34 |
154.45 |
153.46 |
153.93 |
---|
stock.loc['2017-06-01':'2017-06-05', ['Open', 'Close']]
|
Open |
Close |
---|
Date |
|
|
---|
2017-06-01 |
153.17 |
153.18 |
---|
2017-06-02 |
153.58 |
155.45 |
---|
2017-06-05 |
154.34 |
153.93 |
---|
# :表示的是从一项到另外一项里的所有内容。 ,表示这两项里的内容
stock.loc['2017-06-01':'2017-06-05', ['Open', 'Close','High']]
|
Open |
Close |
High |
---|
Date |
|
|
|
---|
2017-06-01 |
153.17 |
153.18 |
153.33 |
---|
2017-06-02 |
153.58 |
155.45 |
155.45 |
---|
2017-06-05 |
154.34 |
153.93 |
154.45 |
---|
stock.iloc[0,3]
#没懂
153.18000000000001
stock.iloc[0:2, 0:3]
#这个应该是查找数据的前2行,前3列
|
Open |
High |
Low |
---|
Date |
|
|
|
---|
2017-06-01 |
153.17 |
153.33 |
152.22 |
---|
2017-06-02 |
153.58 |
155.45 |
152.89 |
---|
stock.iloc[0:5, 0:4]
#对了就是的
|
Open |
High |
Low |
Close |
---|
Date |
|
|
|
|
---|
2017-06-01 |
153.17 |
153.33 |
152.22 |
153.18 |
---|
2017-06-02 |
153.58 |
155.45 |
152.89 |
155.45 |
---|
2017-06-05 |
154.34 |
154.45 |
153.46 |
153.93 |
---|
2017-06-06 |
153.90 |
155.81 |
153.78 |
154.45 |
---|
2017-06-07 |
155.02 |
155.98 |
154.48 |
155.37 |
---|
stock.iloc[0:2, :]
#这个是表示前2行,后面没有数字,则表示全部列吧
|
Open |
High |
Low |
Close |
Volume |
---|
Date |
|
|
|
|
|
---|
2017-06-01 |
153.17 |
153.33 |
152.22 |
153.18 |
16404088 |
---|
2017-06-02 |
153.58 |
155.45 |
152.89 |
155.45 |
27770715 |
---|
stock.Volume > 5e7
Date
2017-06-01 False
2017-06-02 False
2017-06-05 False
2017-06-06 False
2017-06-07 False
2017-06-08 False
2017-06-09 True
2017-06-12 True
2017-06-13 False
2017-06-14 False
2017-06-15 False
2017-06-16 True
2017-06-19 False
2017-06-20 False
2017-06-21 False
2017-06-22 False
2017-06-23 False
2017-06-26 False
2017-06-27 False
2017-06-28 False
2017-06-29 False
2017-06-30 False
Name: Volume, dtype: bool
stock.Open > 5e7
#5e7是表示5千万?
Date
2017-06-01 False
2017-06-02 False
2017-06-05 False
2017-06-06 False
2017-06-07 False
2017-06-08 False
2017-06-09 False
2017-06-12 False
2017-06-13 False
2017-06-14 False
2017-06-15 False
2017-06-16 False
2017-06-19 False
2017-06-20 False
2017-06-21 False
2017-06-22 False
2017-06-23 False
2017-06-26 False
2017-06-27 False
2017-06-28 False
2017-06-29 False
2017-06-30 False
Name: Open, dtype: bool
stock[stock.Volume > 5e7]
|
Open |
High |
Low |
Close |
Volume |
---|
Date |
|
|
|
|
|
---|
2017-06-09 |
155.19 |
155.19 |
146.02 |
148.98 |
64882657 |
---|
2017-06-12 |
145.74 |
146.09 |
142.51 |
145.42 |
72307330 |
---|
2017-06-16 |
143.78 |
144.50 |
142.20 |
142.27 |
50361093 |
---|
stock[stock.Volume < 5e7]
|
Open |
High |
Low |
Close |
Volume |
---|
Date |
|
|
|
|
|
---|
2017-06-01 |
153.17 |
153.33 |
152.22 |
153.18 |
16404088 |
---|
2017-06-02 |
153.58 |
155.45 |
152.89 |
155.45 |
27770715 |
---|
2017-06-05 |
154.34 |
154.45 |
153.46 |
153.93 |
25331662 |
---|
2017-06-06 |
153.90 |
155.81 |
153.78 |
154.45 |
26624926 |
---|
2017-06-07 |
155.02 |
155.98 |
154.48 |
155.37 |
21069647 |
---|
2017-06-08 |
155.25 |
155.54 |
154.40 |
154.99 |
21250798 |
---|
2017-06-13 |
147.16 |
147.45 |
145.15 |
146.59 |
34165445 |
---|
2017-06-14 |
147.50 |
147.50 |
143.84 |
145.16 |
31531232 |
---|
2017-06-15 |
143.32 |
144.48 |
142.21 |
144.29 |
32165373 |
---|
2017-06-19 |
143.66 |
146.74 |
143.66 |
146.34 |
32541404 |
---|
2017-06-20 |
146.87 |
146.87 |
144.94 |
145.01 |
24900073 |
---|
2017-06-21 |
145.52 |
146.07 |
144.61 |
145.87 |
21265751 |
---|
2017-06-22 |
145.77 |
146.70 |
145.12 |
145.63 |
19106294 |
---|
2017-06-23 |
145.13 |
147.16 |
145.11 |
146.28 |
35439389 |
---|
2017-06-26 |
147.17 |
148.28 |
145.38 |
145.82 |
25692361 |
---|
2017-06-27 |
145.01 |
146.16 |
143.62 |
143.73 |
24761891 |
---|
2017-06-28 |
144.49 |
146.11 |
143.16 |
145.83 |
22082432 |
---|
2017-06-29 |
144.71 |
145.13 |
142.28 |
143.68 |
31499368 |
---|
2017-06-30 |
144.45 |
144.96 |
143.78 |
144.02 |
23024107 |
---|
stock[stock.Volume> 5e7]
#加中括号应该是精确显示大于5千万的所有数据吧
|
Open |
High |
Low |
Close |
Volume |
---|
Date |
|
|
|
|
|
---|
2017-06-09 |
155.19 |
155.19 |
146.02 |
148.98 |
64882657 |
---|
2017-06-12 |
145.74 |
146.09 |
142.51 |
145.42 |
72307330 |
---|
2017-06-16 |
143.78 |
144.50 |
142.20 |
142.27 |
50361093 |
---|
stock.Open>5e7
Date
2017-06-01 False
2017-06-02 False
2017-06-05 False
2017-06-06 False
2017-06-07 False
2017-06-08 False
2017-06-09 False
2017-06-12 False
2017-06-13 False
2017-06-14 False
2017-06-15 False
2017-06-16 False
2017-06-19 False
2017-06-20 False
2017-06-21 False
2017-06-22 False
2017-06-23 False
2017-06-26 False
2017-06-27 False
2017-06-28 False
2017-06-29 False
2017-06-30 False
Name: Open, dtype: bool
stock[stock.Close > stock.Open]
|
Open |
High |
Low |
Close |
Volume |
---|
Date |
|
|
|
|
|
---|
2017-06-01 |
153.17 |
153.33 |
152.22 |
153.18 |
16404088 |
---|
2017-06-02 |
153.58 |
155.45 |
152.89 |
155.45 |
27770715 |
---|
2017-06-06 |
153.90 |
155.81 |
153.78 |
154.45 |
26624926 |
---|
2017-06-07 |
155.02 |
155.98 |
154.48 |
155.37 |
21069647 |
---|
2017-06-15 |
143.32 |
144.48 |
142.21 |
144.29 |
32165373 |
---|
2017-06-19 |
143.66 |
146.74 |
143.66 |
146.34 |
32541404 |
---|
2017-06-21 |
145.52 |
146.07 |
144.61 |
145.87 |
21265751 |
---|
2017-06-23 |
145.13 |
147.16 |
145.11 |
146.28 |
35439389 |
---|
2017-06-28 |
144.49 |
146.11 |
143.16 |
145.83 |
22082432 |
---|
stock[(stock.Close > stock.Open) & (stock.Volume > 3e7)]
#没懂
|
Open |
High |
Low |
Close |
Volume |
---|
Date |
|
|
|
|
|
---|
2017-06-15 |
143.32 |
144.48 |
142.21 |
144.29 |
32165373 |
---|
2017-06-19 |
143.66 |
146.74 |
143.66 |
146.34 |
32541404 |
---|
2017-06-23 |
145.13 |
147.16 |
145.11 |
146.28 |
35439389 |
---|
stock[(stock.Close > stock.Open) | (stock.Volume > 5e7)]
|
Open |
High |
Low |
Close |
Volume |
---|
Date |
|
|
|
|
|
---|
2017-06-01 |
153.17 |
153.33 |
152.22 |
153.18 |
16404088 |
---|
2017-06-02 |
153.58 |
155.45 |
152.89 |
155.45 |
27770715 |
---|
2017-06-06 |
153.90 |
155.81 |
153.78 |
154.45 |
26624926 |
---|
2017-06-07 |
155.02 |
155.98 |
154.48 |
155.37 |
21069647 |
---|
2017-06-09 |
155.19 |
155.19 |
146.02 |
148.98 |
64882657 |
---|
2017-06-12 |
145.74 |
146.09 |
142.51 |
145.42 |
72307330 |
---|
2017-06-15 |
143.32 |
144.48 |
142.21 |
144.29 |
32165373 |
---|
2017-06-16 |
143.78 |
144.50 |
142.20 |
142.27 |
50361093 |
---|
2017-06-19 |
143.66 |
146.74 |
143.66 |
146.34 |
32541404 |
---|
2017-06-21 |
145.52 |
146.07 |
144.61 |
145.87 |
21265751 |
---|
2017-06-23 |
145.13 |
147.16 |
145.11 |
146.28 |
35439389 |
---|
2017-06-28 |
144.49 |
146.11 |
143.16 |
145.83 |
22082432 |
---|
stock[(stock.Close > stock.Open) | (stock.Volume > 5e7)|(stock.Low)>5e7]
#自己弄了一个,感觉没对
|
Open |
High |
Low |
Close |
Volume |
---|
Date |
|
|
|
|
|
---|
stock['fluctuation'] = stock['High'] - stock['Low']
stock.head()
#波动
|
Open |
High |
Low |
Close |
Volume |
fluctuation |
---|
Date |
|
|
|
|
|
|
---|
2017-06-01 |
153.17 |
153.33 |
152.22 |
153.18 |
16404088 |
1.11 |
---|
2017-06-02 |
153.58 |
155.45 |
152.89 |
155.45 |
27770715 |
2.56 |
---|
2017-06-05 |
154.34 |
154.45 |
153.46 |
153.93 |
25331662 |
0.99 |
---|
2017-06-06 |
153.90 |
155.81 |
153.78 |
154.45 |
26624926 |
2.03 |
---|
2017-06-07 |
155.02 |
155.98 |
154.48 |
155.37 |
21069647 |
1.50 |
---|
stock['Close'].shift(1)
#没懂
Date
2017-06-01 NaN
2017-06-02 153.18
2017-06-05 155.45
2017-06-06 153.93
2017-06-07 154.45
2017-06-08 155.37
2017-06-09 154.99
2017-06-12 148.98
2017-06-13 145.42
2017-06-14 146.59
2017-06-15 145.16
2017-06-16 144.29
2017-06-19 142.27
2017-06-20 146.34
2017-06-21 145.01
2017-06-22 145.87
2017-06-23 145.63
2017-06-26 146.28
2017-06-27 145.82
2017-06-28 143.73
2017-06-29 145.83
2017-06-30 143.68
Name: Close, dtype: float64
stock['Change'] = stock['Close'] - stock['Close'].shift(1)
stock.head()
|
Open |
High |
Low |
Close |
Volume |
fluctuation |
Change |
---|
Date |
|
|
|
|
|
|
|
---|
2017-06-01 |
153.17 |
153.33 |
152.22 |
153.18 |
16404088 |
1.11 |
NaN |
---|
2017-06-02 |
153.58 |
155.45 |
152.89 |
155.45 |
27770715 |
2.56 |
2.27 |
---|
2017-06-05 |
154.34 |
154.45 |
153.46 |
153.93 |
25331662 |
0.99 |
-1.52 |
---|
2017-06-06 |
153.90 |
155.81 |
153.78 |
154.45 |
26624926 |
2.03 |
0.52 |
---|
2017-06-07 |
155.02 |
155.98 |
154.48 |
155.37 |
21069647 |
1.50 |
0.92 |
---|
stock['Change'] = stock['Close'] - stock['Close'].shift(2)
stock.head()
#没懂
|
Open |
High |
Low |
Close |
Volume |
fluctuation |
Change |
---|
Date |
|
|
|
|
|
|
|
---|
2017-06-01 |
153.17 |
153.33 |
152.22 |
153.18 |
16404088 |
1.11 |
NaN |
---|
2017-06-02 |
153.58 |
155.45 |
152.89 |
155.45 |
27770715 |
2.56 |
NaN |
---|
2017-06-05 |
154.34 |
154.45 |
153.46 |
153.93 |
25331662 |
0.99 |
0.75 |
---|
2017-06-06 |
153.90 |
155.81 |
153.78 |
154.45 |
26624926 |
2.03 |
-1.00 |
---|
2017-06-07 |
155.02 |
155.98 |
154.48 |
155.37 |
21069647 |
1.50 |
1.44 |
---|
#第一题:2017年6月22日当天的收盘价是多少?
stock['Close']['2017-06-22']
145.63
#第二题: 成交量超过7千万的是哪一天?
stock.[stock.Volume>5e7]
File "", line 1
stock.[stock.Volume>5e7]
^
SyntaxError: invalid syntax
stock.Volume>7e7
Date
2017-06-01 False
2017-06-02 False
2017-06-05 False
2017-06-06 False
2017-06-07 False
2017-06-08 False
2017-06-09 False
2017-06-12 True
2017-06-13 False
2017-06-14 False
2017-06-15 False
2017-06-16 False
2017-06-19 False
2017-06-20 False
2017-06-21 False
2017-06-22 False
2017-06-23 False
2017-06-26 False
2017-06-27 False
2017-06-28 False
2017-06-29 False
2017-06-30 False
Name: Volume, dtype: bool
#第三题:在2017年6月28日,收盘价减去开盘价是多少?
stock['2017-06-28']['Close'-'Open']
#没做出来
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
2392 try:
-> 2393 return self._engine.get_loc(key)
2394 except KeyError:
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:5239)()
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:5085)()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas\_libs\hashtable.c:20405)()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas\_libs\hashtable.c:20359)()
KeyError: '2017-06-28'
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call last)
in ()
----> 1 stock['2017-06-28']['Close'-'Open']
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
2060 return self._getitem_multilevel(key)
2061 else:
-> 2062 return self._getitem_column(key)
2063
2064 def _getitem_column(self, key):
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py in _getitem_column(self, key)
2067 # get column
2068 if self.columns.is_unique:
-> 2069 return self._get_item_cache(key)
2070
2071 # duplicate columns & possible reduce dimensionality
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\generic.py in _get_item_cache(self, item)
1532 res = cache.get(item)
1533 if res is None:
-> 1534 values = self._data.get(item)
1535 res = self._box_item_values(item, values)
1536 cache[item] = res
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\internals.py in get(self, item, fastpath)
3588
3589 if not isnull(item):
-> 3590 loc = self.items.get_loc(item)
3591 else:
3592 indexer = np.arange(len(self.items))[isnull(self.items)]
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
2393 return self._engine.get_loc(key)
2394 except KeyError:
-> 2395 return self._engine.get_loc(self._maybe_cast_indexer(key))
2396
2397 indexer = self.get_indexer([key], method=method, tolerance=tolerance)
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:5239)()
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:5085)()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas\_libs\hashtable.c:20405)()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas\_libs\hashtable.c:20359)()
KeyError: '2017-06-28'
#第四题: 开盘价低于144并且成交量大于5千万那一天的收盘价是多少?
stock[(stock.Open<144)&(stock.Volume>5e7)]
|
Open |
High |
Low |
Close |
Volume |
fluctuation |
Change |
---|
Date |
|
|
|
|
|
|
|
---|
2017-06-16 |
143.78 |
144.5 |
142.2 |
142.27 |
50361093 |
2.3 |
-2.89 |
---|
#第五题: 课程中计算的涨跌幅change的最大值是多少?
stock.change
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
in ()
----> 1 stock.change.max()
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\generic.py in __getattr__(self, name)
2968 if name in self._info_axis:
2969 return self[name]
-> 2970 return object.__getattribute__(self, name)
2971
2972 def __setattr__(self, name, value):
AttributeError: 'DataFrame' object has no attribute 'change'
stock['Change'] =['stock.Open':'stock.Volume'>max]
#没做来
File "", line 1
stock['Change'] =['stock.Open':'stock.Volume'>max]
^
SyntaxError: invalid syntax
#第一课都走了一遍,还是学到一些东西,但是还是很多东西没有学懂。