# 导入 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这个文件
<div>
<style>
.dataframe thead tr:only-child th {
text-align: right;
}
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
</style>
<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.head()
#head是显示头部数据,默认是前五个数据
<div>
<style>
.dataframe thead tr:only-child th {
text-align: right;
}
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
</style>
<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)
#()里面可以设置你需要查找的数量,比如我需要查找前3个的数据,只需要在()里输入3就可以了
<div>
<style>
.dataframe thead tr:only-child th {
text-align: right;
}
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
</style>
<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>
stock.tail()
#tail是显示尾部的数据,默认就是最后5个数据。()也是同上面的一样,输入数字,代表是查找对应最后几个的数据。
<div>
<style>
.dataframe thead tr:only-child th {
text-align: right;
}
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
</style>
<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.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()
#我的理解就是像查看文件属性是一样的
<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.info(10)
#后面的()有何意义呢?
<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>
<style>
.dataframe thead tr:only-child th {
text-align: right;
}
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
</style>
<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.plot()
#绘图,折线图
<matplotlib.axes._subplots.AxesSubplot at 0x9357400>
stock.plot(y='Open')
#()里设置要显示的折线图数据
<matplotlib.axes._subplots.AxesSubplot at 0x44bc9e8>
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']差不多,只是显示出来不一样
<div>
<style>
.dataframe thead tr:only-child th {
text-align: right;
}
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
</style>
<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.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这两项
<div>
<style>
.dataframe thead tr:only-child th {
text-align: right;
}
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
</style>
<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[:,'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']
<div>
<style>
.dataframe thead tr:only-child th {
text-align: right;
}
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
</style>
<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>
<style>
.dataframe thead tr:only-child th {
text-align: right;
}
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
</style>
<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.loc['2017-06-01':'2017-06-05', ['Open', 'Close','High']]
<div>
<style>
.dataframe thead tr:only-child th {
text-align: right;
}
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Open</th>
<th>Close</th>
<th>High</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.18</td>
<td>153.33</td>
</tr>
<tr>
<th>2017-06-02</th>
<td>153.58</td>
<td>155.45</td>
<td>155.45</td>
</tr>
<tr>
<th>2017-06-05</th>
<td>154.34</td>
<td>153.93</td>
<td>154.45</td>
</tr>
</tbody>
</table>
</div>
stock.iloc[0,3]
#没懂
153.18000000000001
stock.iloc[0:2, 0:3]
#这个应该是查找数据的前2行,前3列
<div>
<style>
.dataframe thead tr:only-child th {
text-align: right;
}
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
</style>
<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:5, 0:4]
#对了就是的
<div>
<style>
.dataframe thead tr:only-child th {
text-align: right;
}
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
</style>
<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>
<tr>
<th>2017-06-06</th>
<td>153.90</td>
<td>155.81</td>
<td>153.78</td>
<td>154.45</td>
</tr>
<tr>
<th>2017-06-07</th>
<td>155.02</td>
<td>155.98</td>
<td>154.48</td>
<td>155.37</td>
</tr>
</tbody>
</table>
</div>
stock.iloc[0:2, :]
#这个是表示前2行,后面没有数字,则表示全部列吧
<div>
<style>
.dataframe thead tr:only-child th {
text-align: right;
}
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
</style>
<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.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]
<div>
<style>
.dataframe thead tr:only-child th {
text-align: right;
}
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
</style>
<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.Volume < 5e7]
<div>
<style>
.dataframe thead tr:only-child th {
text-align: right;
}
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
</style>
<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-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-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[stock.Volume> 5e7]
#加中括号应该是精确显示大于5千万的所有数据吧
<div>
<style>
.dataframe thead tr:only-child th {
text-align: right;
}
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
</style>
<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.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]
<div>
<style>
.dataframe thead tr:only-child th {
text-align: right;
}
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
</style>
<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>
<style>
.dataframe thead tr:only-child th {
text-align: right;
}
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
</style>
<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>
<style>
.dataframe thead tr:only-child th {
text-align: right;
}
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
</style>
<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[(stock.Close > stock.Open) | (stock.Volume > 5e7)|(stock.Low)>5e7]
#自己弄了一个,感觉没对
<div>
<style>
.dataframe thead tr:only-child th {
text-align: right;
}
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
</style>
<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>
</tbody>
</table>
</div>
stock['fluctuation'] = stock['High'] - stock['Low']
stock.head()
#波动
<div>
<style>
.dataframe thead tr:only-child th {
text-align: right;
}
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
</style>
<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>
<style>
.dataframe thead tr:only-child th {
text-align: right;
}
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
</style>
<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>
stock['Change'] = stock['Close'] - stock['Close'].shift(2)
stock.head()
#没懂
<div>
<style>
.dataframe thead tr:only-child th {
text-align: right;
}
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
</style>
<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>NaN</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>0.75</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>-1.00</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>1.44</td>
</tr>
</tbody>
</table>
</div>
#第一题:2017年6月22日当天的收盘价是多少?
stock['Close']['2017-06-22']
145.63
#第二题: 成交量超过7千万的是哪一天?
stock.[stock.Volume>5e7]
File "<ipython-input-62-699eabb80231>", 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)
<ipython-input-72-cd81ee331a30> in <module>()
----> 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)]
<div>
<style>
.dataframe thead tr:only-child th {
text-align: right;
}
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
</style>
<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.89</td>
</tr>
</tbody>
</table>
</div>
#第五题: 课程中计算的涨跌幅change的最大值是多少?
stock.change
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-86-499bb8ff3134> in <module>()
----> 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 "<ipython-input-89-aa7cc373d074>", line 1
stock['Change'] =['stock.Open':'stock.Volume'>max]
^
SyntaxError: invalid syntax
#第一课都走了一遍,还是学到一些东西,但是还是很多东西没有学懂。