# 导入 pandas 模块
import pandas as pd
# 设置直接显示图片
%matplotlib inline
stock = pd.read_csv('stock.csv', parse_dates=True, index_col='Date')
stock
<div>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Open</th>
<th>High</th>
<th>Low</th>
<th>Close</th>
<th>Volume</th>
</tr>
<tr>
<th>Date</th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th>2017-06-01</th>
<td>153.17</td>
<td>153.33</td>
<td>152.22</td>
<td>153.18</td>
<td>16404088</td>
</tr>
<tr>
<th>2017-06-02</th>
<td>153.58</td>
<td>155.45</td>
<td>152.89</td>
<td>155.45</td>
<td>27770715</td>
</tr>
<tr>
<th>2017-06-05</th>
<td>154.34</td>
<td>154.45</td>
<td>153.46</td>
<td>153.93</td>
<td>25331662</td>
</tr>
<tr>
<th>2017-06-06</th>
<td>153.90</td>
<td>155.81</td>
<td>153.78</td>
<td>154.45</td>
<td>26624926</td>
</tr>
<tr>
<th>2017-06-07</th>
<td>155.02</td>
<td>155.98</td>
<td>154.48</td>
<td>155.37</td>
<td>21069647</td>
</tr>
<tr>
<th>2017-06-08</th>
<td>155.25</td>
<td>155.54</td>
<td>154.40</td>
<td>154.99</td>
<td>21250798</td>
</tr>
<tr>
<th>2017-06-09</th>
<td>155.19</td>
<td>155.19</td>
<td>146.02</td>
<td>148.98</td>
<td>64882657</td>
</tr>
<tr>
<th>2017-06-12</th>
<td>145.74</td>
<td>146.09</td>
<td>142.51</td>
<td>145.42</td>
<td>72307330</td>
</tr>
<tr>
<th>2017-06-13</th>
<td>147.16</td>
<td>147.45</td>
<td>145.15</td>
<td>146.59</td>
<td>34165445</td>
</tr>
<tr>
<th>2017-06-14</th>
<td>147.50</td>
<td>147.50</td>
<td>143.84</td>
<td>145.16</td>
<td>31531232</td>
</tr>
<tr>
<th>2017-06-15</th>
<td>143.32</td>
<td>144.48</td>
<td>142.21</td>
<td>144.29</td>
<td>32165373</td>
</tr>
<tr>
<th>2017-06-16</th>
<td>143.78</td>
<td>144.50</td>
<td>142.20</td>
<td>142.27</td>
<td>50361093</td>
</tr>
<tr>
<th>2017-06-19</th>
<td>143.66</td>
<td>146.74</td>
<td>143.66</td>
<td>146.34</td>
<td>32541404</td>
</tr>
<tr>
<th>2017-06-20</th>
<td>146.87</td>
<td>146.87</td>
<td>144.94</td>
<td>145.01</td>
<td>24900073</td>
</tr>
<tr>
<th>2017-06-21</th>
<td>145.52</td>
<td>146.07</td>
<td>144.61</td>
<td>145.87</td>
<td>21265751</td>
</tr>
<tr>
<th>2017-06-22</th>
<td>145.77</td>
<td>146.70</td>
<td>145.12</td>
<td>145.63</td>
<td>19106294</td>
</tr>
<tr>
<th>2017-06-23</th>
<td>145.13</td>
<td>147.16</td>
<td>145.11</td>
<td>146.28</td>
<td>35439389</td>
</tr>
<tr>
<th>2017-06-26</th>
<td>147.17</td>
<td>148.28</td>
<td>145.38</td>
<td>145.82</td>
<td>25692361</td>
</tr>
<tr>
<th>2017-06-27</th>
<td>145.01</td>
<td>146.16</td>
<td>143.62</td>
<td>143.73</td>
<td>24761891</td>
</tr>
<tr>
<th>2017-06-28</th>
<td>144.49</td>
<td>146.11</td>
<td>143.16</td>
<td>145.83</td>
<td>22082432</td>
</tr>
<tr>
<th>2017-06-29</th>
<td>144.71</td>
<td>145.13</td>
<td>142.28</td>
<td>143.68</td>
<td>31499368</td>
</tr>
<tr>
<th>2017-06-30</th>
<td>144.45</td>
<td>144.96</td>
<td>143.78</td>
<td>144.02</td>
<td>23024107</td>
</tr>
</tbody>
</table>
</div>
import pandas as pd
import datetime
import pandas_datareader.data as web
# 设置获取的时间区间
start = datetime.datetime(2017,6,1)
end = datetime.datetime(2017,6,30)
# 从google获取苹果的股票数据
apple = web.DataReader("AAPL", "google", start, end)
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-10-37ab99df9f31> in <module>()
1 import pandas as pd
2 import datetime
----> 3 import pandas_datareader.data as web
4
5 # 设置获取的时间区间
ModuleNotFoundError: No module named 'pandas_datareader'
stock.head()
<div>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Open</th>
<th>High</th>
<th>Low</th>
<th>Close</th>
<th>Volume</th>
</tr>
<tr>
<th>Date</th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th>2017-06-01</th>
<td>153.17</td>
<td>153.33</td>
<td>152.22</td>
<td>153.18</td>
<td>16404088</td>
</tr>
<tr>
<th>2017-06-02</th>
<td>153.58</td>
<td>155.45</td>
<td>152.89</td>
<td>155.45</td>
<td>27770715</td>
</tr>
<tr>
<th>2017-06-05</th>
<td>154.34</td>
<td>154.45</td>
<td>153.46</td>
<td>153.93</td>
<td>25331662</td>
</tr>
<tr>
<th>2017-06-06</th>
<td>153.90</td>
<td>155.81</td>
<td>153.78</td>
<td>154.45</td>
<td>26624926</td>
</tr>
<tr>
<th>2017-06-07</th>
<td>155.02</td>
<td>155.98</td>
<td>154.48</td>
<td>155.37</td>
<td>21069647</td>
</tr>
</tbody>
</table>
</div>
stock.head(3)
<div>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Open</th>
<th>High</th>
<th>Low</th>
<th>Close</th>
<th>Volume</th>
</tr>
<tr>
<th>Date</th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th>2017-06-01</th>
<td>153.17</td>
<td>153.33</td>
<td>152.22</td>
<td>153.18</td>
<td>16404088</td>
</tr>
<tr>
<th>2017-06-02</th>
<td>153.58</td>
<td>155.45</td>
<td>152.89</td>
<td>155.45</td>
<td>27770715</td>
</tr>
<tr>
<th>2017-06-05</th>
<td>154.34</td>
<td>154.45</td>
<td>153.46</td>
<td>153.93</td>
<td>25331662</td>
</tr>
</tbody>
</table>
</div>
###看尾部5行
stock.tail()
<div>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Open</th>
<th>High</th>
<th>Low</th>
<th>Close</th>
<th>Volume</th>
</tr>
<tr>
<th>Date</th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th>2017-06-26</th>
<td>147.17</td>
<td>148.28</td>
<td>145.38</td>
<td>145.82</td>
<td>25692361</td>
</tr>
<tr>
<th>2017-06-27</th>
<td>145.01</td>
<td>146.16</td>
<td>143.62</td>
<td>143.73</td>
<td>24761891</td>
</tr>
<tr>
<th>2017-06-28</th>
<td>144.49</td>
<td>146.11</td>
<td>143.16</td>
<td>145.83</td>
<td>22082432</td>
</tr>
<tr>
<th>2017-06-29</th>
<td>144.71</td>
<td>145.13</td>
<td>142.28</td>
<td>143.68</td>
<td>31499368</td>
</tr>
<tr>
<th>2017-06-30</th>
<td>144.45</td>
<td>144.96</td>
<td>143.78</td>
<td>144.02</td>
<td>23024107</td>
</tr>
</tbody>
</table>
</div>
#### 总体信息,没有输括号
stock.info
<bound method DataFrame.info of 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.info()
<class 'pandas.core.frame.DataFrame'>
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()
#### 尝试了不输入圆括号,结果就是显示明细
<div>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Open</th>
<th>High</th>
<th>Low</th>
<th>Close</th>
<th>Volume</th>
</tr>
</thead>
<tbody>
<tr>
<th>count</th>
<td>22.000000</td>
<td>22.000000</td>
<td>22.000000</td>
<td>22.000000</td>
<td>2.200000e+01</td>
</tr>
<tr>
<th>mean</th>
<td>148.215000</td>
<td>149.088636</td>
<td>146.582727</td>
<td>147.831364</td>
<td>3.109900e+07</td>
</tr>
<tr>
<th>std</th>
<td>4.450871</td>
<td>4.337027</td>
<td>4.507623</td>
<td>4.434315</td>
<td>1.416738e+07</td>
</tr>
<tr>
<th>min</th>
<td>143.320000</td>
<td>144.480000</td>
<td>142.200000</td>
<td>142.270000</td>
<td>1.640409e+07</td>
</tr>
<tr>
<th>25%</th>
<td>144.785000</td>
<td>146.095000</td>
<td>143.630000</td>
<td>145.047500</td>
<td>2.231785e+07</td>
</tr>
<tr>
<th>50%</th>
<td>146.320000</td>
<td>147.015000</td>
<td>145.025000</td>
<td>145.850000</td>
<td>2.615864e+07</td>
</tr>
<tr>
<th>75%</th>
<td>153.477500</td>
<td>154.170000</td>
<td>150.670000</td>
<td>152.130000</td>
<td>3.244740e+07</td>
</tr>
<tr>
<th>max</th>
<td>155.250000</td>
<td>155.980000</td>
<td>154.480000</td>
<td>155.450000</td>
<td>7.230733e+07</td>
</tr>
</tbody>
</table>
</div>
stock
<div>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Open</th>
<th>High</th>
<th>Low</th>
<th>Close</th>
<th>Volume</th>
</tr>
<tr>
<th>Date</th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th>2017-06-01</th>
<td>153.17</td>
<td>153.33</td>
<td>152.22</td>
<td>153.18</td>
<td>16404088</td>
</tr>
<tr>
<th>2017-06-02</th>
<td>153.58</td>
<td>155.45</td>
<td>152.89</td>
<td>155.45</td>
<td>27770715</td>
</tr>
<tr>
<th>2017-06-05</th>
<td>154.34</td>
<td>154.45</td>
<td>153.46</td>
<td>153.93</td>
<td>25331662</td>
</tr>
<tr>
<th>2017-06-06</th>
<td>153.90</td>
<td>155.81</td>
<td>153.78</td>
<td>154.45</td>
<td>26624926</td>
</tr>
<tr>
<th>2017-06-07</th>
<td>155.02</td>
<td>155.98</td>
<td>154.48</td>
<td>155.37</td>
<td>21069647</td>
</tr>
<tr>
<th>2017-06-08</th>
<td>155.25</td>
<td>155.54</td>
<td>154.40</td>
<td>154.99</td>
<td>21250798</td>
</tr>
<tr>
<th>2017-06-09</th>
<td>155.19</td>
<td>155.19</td>
<td>146.02</td>
<td>148.98</td>
<td>64882657</td>
</tr>
<tr>
<th>2017-06-12</th>
<td>145.74</td>
<td>146.09</td>
<td>142.51</td>
<td>145.42</td>
<td>72307330</td>
</tr>
<tr>
<th>2017-06-13</th>
<td>147.16</td>
<td>147.45</td>
<td>145.15</td>
<td>146.59</td>
<td>34165445</td>
</tr>
<tr>
<th>2017-06-14</th>
<td>147.50</td>
<td>147.50</td>
<td>143.84</td>
<td>145.16</td>
<td>31531232</td>
</tr>
<tr>
<th>2017-06-15</th>
<td>143.32</td>
<td>144.48</td>
<td>142.21</td>
<td>144.29</td>
<td>32165373</td>
</tr>
<tr>
<th>2017-06-16</th>
<td>143.78</td>
<td>144.50</td>
<td>142.20</td>
<td>142.27</td>
<td>50361093</td>
</tr>
<tr>
<th>2017-06-19</th>
<td>143.66</td>
<td>146.74</td>
<td>143.66</td>
<td>146.34</td>
<td>32541404</td>
</tr>
<tr>
<th>2017-06-20</th>
<td>146.87</td>
<td>146.87</td>
<td>144.94</td>
<td>145.01</td>
<td>24900073</td>
</tr>
<tr>
<th>2017-06-21</th>
<td>145.52</td>
<td>146.07</td>
<td>144.61</td>
<td>145.87</td>
<td>21265751</td>
</tr>
<tr>
<th>2017-06-22</th>
<td>145.77</td>
<td>146.70</td>
<td>145.12</td>
<td>145.63</td>
<td>19106294</td>
</tr>
<tr>
<th>2017-06-23</th>
<td>145.13</td>
<td>147.16</td>
<td>145.11</td>
<td>146.28</td>
<td>35439389</td>
</tr>
<tr>
<th>2017-06-26</th>
<td>147.17</td>
<td>148.28</td>
<td>145.38</td>
<td>145.82</td>
<td>25692361</td>
</tr>
<tr>
<th>2017-06-27</th>
<td>145.01</td>
<td>146.16</td>
<td>143.62</td>
<td>143.73</td>
<td>24761891</td>
</tr>
<tr>
<th>2017-06-28</th>
<td>144.49</td>
<td>146.11</td>
<td>143.16</td>
<td>145.83</td>
<td>22082432</td>
</tr>
<tr>
<th>2017-06-29</th>
<td>144.71</td>
<td>145.13</td>
<td>142.28</td>
<td>143.68</td>
<td>31499368</td>
</tr>
<tr>
<th>2017-06-30</th>
<td>144.45</td>
<td>144.96</td>
<td>143.78</td>
<td>144.02</td>
<td>23024107</td>
</tr>
</tbody>
</table>
</div>
stock.shape
(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.plot()
<matplotlib.axes._subplots.AxesSubplot at 0x23bdac3a3c8>
stock.plot(y='Open')
<matplotlib.axes._subplots.AxesSubplot at 0x23bdb08e748>
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
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-01']
153.18000000000001
stock['Close'][0]
153.18000000000001
stock[['Close']]
<div>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Close</th>
</tr>
<tr>
<th>Date</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th>2017-06-01</th>
<td>153.18</td>
</tr>
<tr>
<th>2017-06-02</th>
<td>155.45</td>
</tr>
<tr>
<th>2017-06-05</th>
<td>153.93</td>
</tr>
<tr>
<th>2017-06-06</th>
<td>154.45</td>
</tr>
<tr>
<th>2017-06-07</th>
<td>155.37</td>
</tr>
<tr>
<th>2017-06-08</th>
<td>154.99</td>
</tr>
<tr>
<th>2017-06-09</th>
<td>148.98</td>
</tr>
<tr>
<th>2017-06-12</th>
<td>145.42</td>
</tr>
<tr>
<th>2017-06-13</th>
<td>146.59</td>
</tr>
<tr>
<th>2017-06-14</th>
<td>145.16</td>
</tr>
<tr>
<th>2017-06-15</th>
<td>144.29</td>
</tr>
<tr>
<th>2017-06-16</th>
<td>142.27</td>
</tr>
<tr>
<th>2017-06-19</th>
<td>146.34</td>
</tr>
<tr>
<th>2017-06-20</th>
<td>145.01</td>
</tr>
<tr>
<th>2017-06-21</th>
<td>145.87</td>
</tr>
<tr>
<th>2017-06-22</th>
<td>145.63</td>
</tr>
<tr>
<th>2017-06-23</th>
<td>146.28</td>
</tr>
<tr>
<th>2017-06-26</th>
<td>145.82</td>
</tr>
<tr>
<th>2017-06-27</th>
<td>143.73</td>
</tr>
<tr>
<th>2017-06-28</th>
<td>145.83</td>
</tr>
<tr>
<th>2017-06-29</th>
<td>143.68</td>
</tr>
<tr>
<th>2017-06-30</th>
<td>144.02</td>
</tr>
</tbody>
</table>
</div>
stock[['Open','Close']]
<div>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Open</th>
<th>Close</th>
</tr>
<tr>
<th>Date</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th>2017-06-01</th>
<td>153.17</td>
<td>153.18</td>
</tr>
<tr>
<th>2017-06-02</th>
<td>153.58</td>
<td>155.45</td>
</tr>
<tr>
<th>2017-06-05</th>
<td>154.34</td>
<td>153.93</td>
</tr>
<tr>
<th>2017-06-06</th>
<td>153.90</td>
<td>154.45</td>
</tr>
<tr>
<th>2017-06-07</th>
<td>155.02</td>
<td>155.37</td>
</tr>
<tr>
<th>2017-06-08</th>
<td>155.25</td>
<td>154.99</td>
</tr>
<tr>
<th>2017-06-09</th>
<td>155.19</td>
<td>148.98</td>
</tr>
<tr>
<th>2017-06-12</th>
<td>145.74</td>
<td>145.42</td>
</tr>
<tr>
<th>2017-06-13</th>
<td>147.16</td>
<td>146.59</td>
</tr>
<tr>
<th>2017-06-14</th>
<td>147.50</td>
<td>145.16</td>
</tr>
<tr>
<th>2017-06-15</th>
<td>143.32</td>
<td>144.29</td>
</tr>
<tr>
<th>2017-06-16</th>
<td>143.78</td>
<td>142.27</td>
</tr>
<tr>
<th>2017-06-19</th>
<td>143.66</td>
<td>146.34</td>
</tr>
<tr>
<th>2017-06-20</th>
<td>146.87</td>
<td>145.01</td>
</tr>
<tr>
<th>2017-06-21</th>
<td>145.52</td>
<td>145.87</td>
</tr>
<tr>
<th>2017-06-22</th>
<td>145.77</td>
<td>145.63</td>
</tr>
<tr>
<th>2017-06-23</th>
<td>145.13</td>
<td>146.28</td>
</tr>
<tr>
<th>2017-06-26</th>
<td>147.17</td>
<td>145.82</td>
</tr>
<tr>
<th>2017-06-27</th>
<td>145.01</td>
<td>143.73</td>
</tr>
<tr>
<th>2017-06-28</th>
<td>144.49</td>
<td>145.83</td>
</tr>
<tr>
<th>2017-06-29</th>
<td>144.71</td>
<td>143.68</td>
</tr>
<tr>
<th>2017-06-30</th>
<td>144.45</td>
<td>144.02</td>
</tr>
</tbody>
</table>
</div>
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['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-01':'2017-06-05', 'Open':'Close']
<div>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Open</th>
<th>High</th>
<th>Low</th>
<th>Close</th>
</tr>
<tr>
<th>Date</th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th>2017-06-01</th>
<td>153.17</td>
<td>153.33</td>
<td>152.22</td>
<td>153.18</td>
</tr>
<tr>
<th>2017-06-02</th>
<td>153.58</td>
<td>155.45</td>
<td>152.89</td>
<td>155.45</td>
</tr>
<tr>
<th>2017-06-05</th>
<td>154.34</td>
<td>154.45</td>
<td>153.46</td>
<td>153.93</td>
</tr>
</tbody>
</table>
</div>
stock.loc['2017-06-01':'2017-06-05', ['Open', 'Close']]
<div>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Open</th>
<th>Close</th>
</tr>
<tr>
<th>Date</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th>2017-06-01</th>
<td>153.17</td>
<td>153.18</td>
</tr>
<tr>
<th>2017-06-02</th>
<td>153.58</td>
<td>155.45</td>
</tr>
<tr>
<th>2017-06-05</th>
<td>154.34</td>
<td>153.93</td>
</tr>
</tbody>
</table>
</div>
stock.iloc[0,3]
153.18000000000001
stock.iloc[0:2, 0:3]
<div>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Open</th>
<th>High</th>
<th>Low</th>
</tr>
<tr>
<th>Date</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th>2017-06-01</th>
<td>153.17</td>
<td>153.33</td>
<td>152.22</td>
</tr>
<tr>
<th>2017-06-02</th>
<td>153.58</td>
<td>155.45</td>
<td>152.89</td>
</tr>
</tbody>
</table>
</div>
stock.iloc[0:2, :]
<div>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Open</th>
<th>High</th>
<th>Low</th>
<th>Close</th>
<th>Volume</th>
</tr>
<tr>
<th>Date</th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th>2017-06-01</th>
<td>153.17</td>
<td>153.33</td>
<td>152.22</td>
<td>153.18</td>
<td>16404088</td>
</tr>
<tr>
<th>2017-06-02</th>
<td>153.58</td>
<td>155.45</td>
<td>152.89</td>
<td>155.45</td>
<td>27770715</td>
</tr>
</tbody>
</table>
</div>
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[stock.Volume > 5e7]
<div>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Open</th>
<th>High</th>
<th>Low</th>
<th>Close</th>
<th>Volume</th>
</tr>
<tr>
<th>Date</th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th>2017-06-09</th>
<td>155.19</td>
<td>155.19</td>
<td>146.02</td>
<td>148.98</td>
<td>64882657</td>
</tr>
<tr>
<th>2017-06-12</th>
<td>145.74</td>
<td>146.09</td>
<td>142.51</td>
<td>145.42</td>
<td>72307330</td>
</tr>
<tr>
<th>2017-06-16</th>
<td>143.78</td>
<td>144.50</td>
<td>142.20</td>
<td>142.27</td>
<td>50361093</td>
</tr>
</tbody>
</table>
</div>
stock[stock.Close > stock.Open]
<div>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Open</th>
<th>High</th>
<th>Low</th>
<th>Close</th>
<th>Volume</th>
</tr>
<tr>
<th>Date</th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th>2017-06-01</th>
<td>153.17</td>
<td>153.33</td>
<td>152.22</td>
<td>153.18</td>
<td>16404088</td>
</tr>
<tr>
<th>2017-06-02</th>
<td>153.58</td>
<td>155.45</td>
<td>152.89</td>
<td>155.45</td>
<td>27770715</td>
</tr>
<tr>
<th>2017-06-06</th>
<td>153.90</td>
<td>155.81</td>
<td>153.78</td>
<td>154.45</td>
<td>26624926</td>
</tr>
<tr>
<th>2017-06-07</th>
<td>155.02</td>
<td>155.98</td>
<td>154.48</td>
<td>155.37</td>
<td>21069647</td>
</tr>
<tr>
<th>2017-06-15</th>
<td>143.32</td>
<td>144.48</td>
<td>142.21</td>
<td>144.29</td>
<td>32165373</td>
</tr>
<tr>
<th>2017-06-19</th>
<td>143.66</td>
<td>146.74</td>
<td>143.66</td>
<td>146.34</td>
<td>32541404</td>
</tr>
<tr>
<th>2017-06-21</th>
<td>145.52</td>
<td>146.07</td>
<td>144.61</td>
<td>145.87</td>
<td>21265751</td>
</tr>
<tr>
<th>2017-06-23</th>
<td>145.13</td>
<td>147.16</td>
<td>145.11</td>
<td>146.28</td>
<td>35439389</td>
</tr>
<tr>
<th>2017-06-28</th>
<td>144.49</td>
<td>146.11</td>
<td>143.16</td>
<td>145.83</td>
<td>22082432</td>
</tr>
</tbody>
</table>
</div>
stock[(stock.Close > stock.Open) & (stock.Volume > 3e7)]
<div>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Open</th>
<th>High</th>
<th>Low</th>
<th>Close</th>
<th>Volume</th>
</tr>
<tr>
<th>Date</th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th>2017-06-15</th>
<td>143.32</td>
<td>144.48</td>
<td>142.21</td>
<td>144.29</td>
<td>32165373</td>
</tr>
<tr>
<th>2017-06-19</th>
<td>143.66</td>
<td>146.74</td>
<td>143.66</td>
<td>146.34</td>
<td>32541404</td>
</tr>
<tr>
<th>2017-06-23</th>
<td>145.13</td>
<td>147.16</td>
<td>145.11</td>
<td>146.28</td>
<td>35439389</td>
</tr>
</tbody>
</table>
</div>
stock[(stock.Close > stock.Open) | (stock.Volume > 5e7)]
<div>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Open</th>
<th>High</th>
<th>Low</th>
<th>Close</th>
<th>Volume</th>
</tr>
<tr>
<th>Date</th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th>2017-06-01</th>
<td>153.17</td>
<td>153.33</td>
<td>152.22</td>
<td>153.18</td>
<td>16404088</td>
</tr>
<tr>
<th>2017-06-02</th>
<td>153.58</td>
<td>155.45</td>
<td>152.89</td>
<td>155.45</td>
<td>27770715</td>
</tr>
<tr>
<th>2017-06-06</th>
<td>153.90</td>
<td>155.81</td>
<td>153.78</td>
<td>154.45</td>
<td>26624926</td>
</tr>
<tr>
<th>2017-06-07</th>
<td>155.02</td>
<td>155.98</td>
<td>154.48</td>
<td>155.37</td>
<td>21069647</td>
</tr>
<tr>
<th>2017-06-09</th>
<td>155.19</td>
<td>155.19</td>
<td>146.02</td>
<td>148.98</td>
<td>64882657</td>
</tr>
<tr>
<th>2017-06-12</th>
<td>145.74</td>
<td>146.09</td>
<td>142.51</td>
<td>145.42</td>
<td>72307330</td>
</tr>
<tr>
<th>2017-06-15</th>
<td>143.32</td>
<td>144.48</td>
<td>142.21</td>
<td>144.29</td>
<td>32165373</td>
</tr>
<tr>
<th>2017-06-16</th>
<td>143.78</td>
<td>144.50</td>
<td>142.20</td>
<td>142.27</td>
<td>50361093</td>
</tr>
<tr>
<th>2017-06-19</th>
<td>143.66</td>
<td>146.74</td>
<td>143.66</td>
<td>146.34</td>
<td>32541404</td>
</tr>
<tr>
<th>2017-06-21</th>
<td>145.52</td>
<td>146.07</td>
<td>144.61</td>
<td>145.87</td>
<td>21265751</td>
</tr>
<tr>
<th>2017-06-23</th>
<td>145.13</td>
<td>147.16</td>
<td>145.11</td>
<td>146.28</td>
<td>35439389</td>
</tr>
<tr>
<th>2017-06-28</th>
<td>144.49</td>
<td>146.11</td>
<td>143.16</td>
<td>145.83</td>
<td>22082432</td>
</tr>
</tbody>
</table>
</div>
stock['fluctuation'] = stock['High'] - stock['Low']
stock.head()
<div>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Open</th>
<th>High</th>
<th>Low</th>
<th>Close</th>
<th>Volume</th>
<th>fluctuation</th>
</tr>
<tr>
<th>Date</th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th>2017-06-01</th>
<td>153.17</td>
<td>153.33</td>
<td>152.22</td>
<td>153.18</td>
<td>16404088</td>
<td>1.11</td>
</tr>
<tr>
<th>2017-06-02</th>
<td>153.58</td>
<td>155.45</td>
<td>152.89</td>
<td>155.45</td>
<td>27770715</td>
<td>2.56</td>
</tr>
<tr>
<th>2017-06-05</th>
<td>154.34</td>
<td>154.45</td>
<td>153.46</td>
<td>153.93</td>
<td>25331662</td>
<td>0.99</td>
</tr>
<tr>
<th>2017-06-06</th>
<td>153.90</td>
<td>155.81</td>
<td>153.78</td>
<td>154.45</td>
<td>26624926</td>
<td>2.03</td>
</tr>
<tr>
<th>2017-06-07</th>
<td>155.02</td>
<td>155.98</td>
<td>154.48</td>
<td>155.37</td>
<td>21069647</td>
<td>1.50</td>
</tr>
</tbody>
</table>
</div>
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()
<div>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Open</th>
<th>High</th>
<th>Low</th>
<th>Close</th>
<th>Volume</th>
<th>fluctuation</th>
<th>Change</th>
</tr>
<tr>
<th>Date</th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th>2017-06-01</th>
<td>153.17</td>
<td>153.33</td>
<td>152.22</td>
<td>153.18</td>
<td>16404088</td>
<td>1.11</td>
<td>NaN</td>
</tr>
<tr>
<th>2017-06-02</th>
<td>153.58</td>
<td>155.45</td>
<td>152.89</td>
<td>155.45</td>
<td>27770715</td>
<td>2.56</td>
<td>2.27</td>
</tr>
<tr>
<th>2017-06-05</th>
<td>154.34</td>
<td>154.45</td>
<td>153.46</td>
<td>153.93</td>
<td>25331662</td>
<td>0.99</td>
<td>-1.52</td>
</tr>
<tr>
<th>2017-06-06</th>
<td>153.90</td>
<td>155.81</td>
<td>153.78</td>
<td>154.45</td>
<td>26624926</td>
<td>2.03</td>
<td>0.52</td>
</tr>
<tr>
<th>2017-06-07</th>
<td>155.02</td>
<td>155.98</td>
<td>154.48</td>
<td>155.37</td>
<td>21069647</td>
<td>1.50</td>
<td>0.92</td>
</tr>
</tbody>
</table>
</div>
本课作业
说明:以下问题请编写代码获得结果。
第一题:2017年6月22日当天的收盘价是多少?
##我自己输入的是这条:stock.loc['2017-06-22','close'],结果错了.参照前面的作业,输入后面这条
stock['Close']['2017-06-22']
145.63
第二题: 成交量超过7千万的是哪一天?
###我自己先尝试乱输入看看呢:stock['volume'>7e5]
####错得离谱,两点。 1.7千万的科学计数,是7e7; 2.stock.volume>7e7,才是正确的表示,
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
###规则化一下呢,答案是6月12号
stock[stock.Volume> 7e7]
<div>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Open</th>
<th>High</th>
<th>Low</th>
<th>Close</th>
<th>Volume</th>
<th>fluctuation</th>
<th>Change</th>
</tr>
<tr>
<th>Date</th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th>2017-06-12</th>
<td>145.74</td>
<td>146.09</td>
<td>142.51</td>
<td>145.42</td>
<td>72307330</td>
<td>3.58</td>
<td>-3.56</td>
</tr>
</tbody>
</table>
</div>
第三题:在2017年6月28日,收盘价减去开盘价是多少?
###我自己尝试着输入看看:stock[stock.Date='2017-06-28',stock.Close-stock.Open],结果错得离谱
stock['Close']-stock['Open']
###使用笨办法,直接算出所有的来,但是不知道如何筛选。应该可以把这段运算赋值给一个“差值”,然后在从“差值”中筛选
Date
2017-06-01 0.01
2017-06-02 1.87
2017-06-05 -0.41
2017-06-06 0.55
2017-06-07 0.35
2017-06-08 -0.26
2017-06-09 -6.21
2017-06-12 -0.32
2017-06-13 -0.57
2017-06-14 -2.34
2017-06-15 0.97
2017-06-16 -1.51
2017-06-19 2.68
2017-06-20 -1.86
2017-06-21 0.35
2017-06-22 -0.14
2017-06-23 1.15
2017-06-26 -1.35
2017-06-27 -1.28
2017-06-28 1.34
2017-06-29 -1.03
2017-06-30 -0.43
dtype: float64
答案是1.34
第四题: 开盘价低于144并且成交量大于5千万那一天的收盘价是多少?
参考此条stock[(stock.Close > stock.Open) & (stock.Volume > 3e7)]
stock[(stock.Open<144)&(stock.Volume>5e7)]
<div>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Open</th>
<th>High</th>
<th>Low</th>
<th>Close</th>
<th>Volume</th>
<th>fluctuation</th>
<th>Change</th>
</tr>
<tr>
<th>Date</th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th>2017-06-16</th>
<td>143.78</td>
<td>144.5</td>
<td>142.2</td>
<td>142.27</td>
<td>50361093</td>
<td>2.3</td>
<td>-2.02</td>
</tr>
</tbody>
</table>
</div>
stock['Change'] = stock['Close'] - stock['Close'].shift(1)
stock
<div>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Open</th>
<th>High</th>
<th>Low</th>
<th>Close</th>
<th>Volume</th>
<th>fluctuation</th>
<th>Change</th>
</tr>
<tr>
<th>Date</th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th>2017-06-01</th>
<td>153.17</td>
<td>153.33</td>
<td>152.22</td>
<td>153.18</td>
<td>16404088</td>
<td>1.11</td>
<td>NaN</td>
</tr>
<tr>
<th>2017-06-02</th>
<td>153.58</td>
<td>155.45</td>
<td>152.89</td>
<td>155.45</td>
<td>27770715</td>
<td>2.56</td>
<td>2.27</td>
</tr>
<tr>
<th>2017-06-05</th>
<td>154.34</td>
<td>154.45</td>
<td>153.46</td>
<td>153.93</td>
<td>25331662</td>
<td>0.99</td>
<td>-1.52</td>
</tr>
<tr>
<th>2017-06-06</th>
<td>153.90</td>
<td>155.81</td>
<td>153.78</td>
<td>154.45</td>
<td>26624926</td>
<td>2.03</td>
<td>0.52</td>
</tr>
<tr>
<th>2017-06-07</th>
<td>155.02</td>
<td>155.98</td>
<td>154.48</td>
<td>155.37</td>
<td>21069647</td>
<td>1.50</td>
<td>0.92</td>
</tr>
<tr>
<th>2017-06-08</th>
<td>155.25</td>
<td>155.54</td>
<td>154.40</td>
<td>154.99</td>
<td>21250798</td>
<td>1.14</td>
<td>-0.38</td>
</tr>
<tr>
<th>2017-06-09</th>
<td>155.19</td>
<td>155.19</td>
<td>146.02</td>
<td>148.98</td>
<td>64882657</td>
<td>9.17</td>
<td>-6.01</td>
</tr>
<tr>
<th>2017-06-12</th>
<td>145.74</td>
<td>146.09</td>
<td>142.51</td>
<td>145.42</td>
<td>72307330</td>
<td>3.58</td>
<td>-3.56</td>
</tr>
<tr>
<th>2017-06-13</th>
<td>147.16</td>
<td>147.45</td>
<td>145.15</td>
<td>146.59</td>
<td>34165445</td>
<td>2.30</td>
<td>1.17</td>
</tr>
<tr>
<th>2017-06-14</th>
<td>147.50</td>
<td>147.50</td>
<td>143.84</td>
<td>145.16</td>
<td>31531232</td>
<td>3.66</td>
<td>-1.43</td>
</tr>
<tr>
<th>2017-06-15</th>
<td>143.32</td>
<td>144.48</td>
<td>142.21</td>
<td>144.29</td>
<td>32165373</td>
<td>2.27</td>
<td>-0.87</td>
</tr>
<tr>
<th>2017-06-16</th>
<td>143.78</td>
<td>144.50</td>
<td>142.20</td>
<td>142.27</td>
<td>50361093</td>
<td>2.30</td>
<td>-2.02</td>
</tr>
<tr>
<th>2017-06-19</th>
<td>143.66</td>
<td>146.74</td>
<td>143.66</td>
<td>146.34</td>
<td>32541404</td>
<td>3.08</td>
<td>4.07</td>
</tr>
<tr>
<th>2017-06-20</th>
<td>146.87</td>
<td>146.87</td>
<td>144.94</td>
<td>145.01</td>
<td>24900073</td>
<td>1.93</td>
<td>-1.33</td>
</tr>
<tr>
<th>2017-06-21</th>
<td>145.52</td>
<td>146.07</td>
<td>144.61</td>
<td>145.87</td>
<td>21265751</td>
<td>1.46</td>
<td>0.86</td>
</tr>
<tr>
<th>2017-06-22</th>
<td>145.77</td>
<td>146.70</td>
<td>145.12</td>
<td>145.63</td>
<td>19106294</td>
<td>1.58</td>
<td>-0.24</td>
</tr>
<tr>
<th>2017-06-23</th>
<td>145.13</td>
<td>147.16</td>
<td>145.11</td>
<td>146.28</td>
<td>35439389</td>
<td>2.05</td>
<td>0.65</td>
</tr>
<tr>
<th>2017-06-26</th>
<td>147.17</td>
<td>148.28</td>
<td>145.38</td>
<td>145.82</td>
<td>25692361</td>
<td>2.90</td>
<td>-0.46</td>
</tr>
<tr>
<th>2017-06-27</th>
<td>145.01</td>
<td>146.16</td>
<td>143.62</td>
<td>143.73</td>
<td>24761891</td>
<td>2.54</td>
<td>-2.09</td>
</tr>
<tr>
<th>2017-06-28</th>
<td>144.49</td>
<td>146.11</td>
<td>143.16</td>
<td>145.83</td>
<td>22082432</td>
<td>2.95</td>
<td>2.10</td>
</tr>
<tr>
<th>2017-06-29</th>
<td>144.71</td>
<td>145.13</td>
<td>142.28</td>
<td>143.68</td>
<td>31499368</td>
<td>2.85</td>
<td>-2.15</td>
</tr>
<tr>
<th>2017-06-30</th>
<td>144.45</td>
<td>144.96</td>
<td>143.78</td>
<td>144.02</td>
<td>23024107</td>
<td>1.18</td>
<td>0.34</td>
</tr>
</tbody>
</table>
</div>