DataFrame的结构
import pandas as pd
import numpy as np
# 创建一个符合正态分布的10个股票的5天的涨跌幅数据
stock_change=np.random.normal(0,1,(10,5))
stock_change
array([[ 1.76548834, -2.1033278 , -0.44765295, 1.6133684 , 0.56440317],
[-0.28406163, 0.33190268, 0.15697138, -0.15352828, 0.39168077],
[ 0.97804543, 0.70247765, 0.48852276, -0.84483313, -0.4901491 ],
[ 0.32663842, -1.18491493, -1.85617695, -0.08334347, -0.88467526],
[-0.0606477 , -0.3282171 , 0.13469079, -0.33644424, 0.49229211],
[-0.08971546, 0.29502656, 0.58225254, -1.15526343, 0.1121633 ],
[ 0.08384949, 0.63896248, 0.03189999, -0.17538923, -0.06095104],
[ 0.0892971 , -1.54426915, 0.22247612, -0.31827644, 1.4569718 ],
[-1.31604909, -1.09219638, -0.64118638, -0.55174267, 2.54943429],
[-0.75827099, 0.19581938, -0.11856612, 0.44903057, -0.07891904]])
# 创建dataframe
stock_num=["股票{}".format(i)for i in range(10)] #构造行标
date=pd.date_range(start="20200629", periods=5,freq="B")
stock_change=pd.DataFrame(stock_change, index=stock_num,columns=date)
stock_change
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
|
2020-06-29 00:00:00 |
2020-06-30 00:00:00 |
2020-07-01 00:00:00 |
2020-07-02 00:00:00 |
2020-07-03 00:00:00 |
股票0 |
1.765488 |
-2.103328 |
-0.447653 |
1.613368 |
0.564403 |
股票1 |
-0.284062 |
0.331903 |
0.156971 |
-0.153528 |
0.391681 |
股票2 |
0.978045 |
0.702478 |
0.488523 |
-0.844833 |
-0.490149 |
股票3 |
0.326638 |
-1.184915 |
-1.856177 |
-0.083343 |
-0.884675 |
股票4 |
-0.060648 |
-0.328217 |
0.134691 |
-0.336444 |
0.492292 |
股票5 |
-0.089715 |
0.295027 |
0.582253 |
-1.155263 |
0.112163 |
股票6 |
0.083849 |
0.638962 |
0.031900 |
-0.175389 |
-0.060951 |
股票7 |
0.089297 |
-1.544269 |
0.222476 |
-0.318276 |
1.456972 |
股票8 |
-1.316049 |
-1.092196 |
-0.641186 |
-0.551743 |
2.549434 |
股票9 |
-0.758271 |
0.195819 |
-0.118566 |
0.449031 |
-0.078919 |
stock_change.shape
(10, 5)
stock_change.index
Index(['股票0', '股票1', '股票2', '股票3', '股票4', '股票5', '股票6', '股票7', '股票8', '股票9'], dtype='object')
stock_change.columns
DatetimeIndex(['2020-06-29', '2020-06-30', '2020-07-01', '2020-07-02',
'2020-07-03'],
dtype='datetime64[ns]', freq='B')
stock_change.values
array([[ 1.76548834, -2.1033278 , -0.44765295, 1.6133684 , 0.56440317],
[-0.28406163, 0.33190268, 0.15697138, -0.15352828, 0.39168077],
[ 0.97804543, 0.70247765, 0.48852276, -0.84483313, -0.4901491 ],
[ 0.32663842, -1.18491493, -1.85617695, -0.08334347, -0.88467526],
[-0.0606477 , -0.3282171 , 0.13469079, -0.33644424, 0.49229211],
[-0.08971546, 0.29502656, 0.58225254, -1.15526343, 0.1121633 ],
[ 0.08384949, 0.63896248, 0.03189999, -0.17538923, -0.06095104],
[ 0.0892971 , -1.54426915, 0.22247612, -0.31827644, 1.4569718 ],
[-1.31604909, -1.09219638, -0.64118638, -0.55174267, 2.54943429],
[-0.75827099, 0.19581938, -0.11856612, 0.44903057, -0.07891904]])
stock_change.T
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
|
股票0 |
股票1 |
股票2 |
股票3 |
股票4 |
股票5 |
股票6 |
股票7 |
股票8 |
股票9 |
2020-06-29 |
1.765488 |
-0.284062 |
0.978045 |
0.326638 |
-0.060648 |
-0.089715 |
0.083849 |
0.089297 |
-1.316049 |
-0.758271 |
2020-06-30 |
-2.103328 |
0.331903 |
0.702478 |
-1.184915 |
-0.328217 |
0.295027 |
0.638962 |
-1.544269 |
-1.092196 |
0.195819 |
2020-07-01 |
-0.447653 |
0.156971 |
0.488523 |
-1.856177 |
0.134691 |
0.582253 |
0.031900 |
0.222476 |
-0.641186 |
-0.118566 |
2020-07-02 |
1.613368 |
-0.153528 |
-0.844833 |
-0.083343 |
-0.336444 |
-1.155263 |
-0.175389 |
-0.318276 |
-0.551743 |
0.449031 |
2020-07-03 |
0.564403 |
0.391681 |
-0.490149 |
-0.884675 |
0.492292 |
0.112163 |
-0.060951 |
1.456972 |
2.549434 |
-0.078919 |
stock_change.head(7)
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
|
2020-06-29 00:00:00 |
2020-06-30 00:00:00 |
2020-07-01 00:00:00 |
2020-07-02 00:00:00 |
2020-07-03 00:00:00 |
股票0 |
1.765488 |
-2.103328 |
-0.447653 |
1.613368 |
0.564403 |
股票1 |
-0.284062 |
0.331903 |
0.156971 |
-0.153528 |
0.391681 |
股票2 |
0.978045 |
0.702478 |
0.488523 |
-0.844833 |
-0.490149 |
股票3 |
0.326638 |
-1.184915 |
-1.856177 |
-0.083343 |
-0.884675 |
股票4 |
-0.060648 |
-0.328217 |
0.134691 |
-0.336444 |
0.492292 |
股票5 |
-0.089715 |
0.295027 |
0.582253 |
-1.155263 |
0.112163 |
股票6 |
0.083849 |
0.638962 |
0.031900 |
-0.175389 |
-0.060951 |
# 索引值修改
new_index=["股票00{}".format(i)for i in range(10)]
stock_change.index=new_index
stock_change
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
|
2020-06-29 00:00:00 |
2020-06-30 00:00:00 |
2020-07-01 00:00:00 |
2020-07-02 00:00:00 |
2020-07-03 00:00:00 |
股票000 |
1.765488 |
-2.103328 |
-0.447653 |
1.613368 |
0.564403 |
股票001 |
-0.284062 |
0.331903 |
0.156971 |
-0.153528 |
0.391681 |
股票002 |
0.978045 |
0.702478 |
0.488523 |
-0.844833 |
-0.490149 |
股票003 |
0.326638 |
-1.184915 |
-1.856177 |
-0.083343 |
-0.884675 |
股票004 |
-0.060648 |
-0.328217 |
0.134691 |
-0.336444 |
0.492292 |
股票005 |
-0.089715 |
0.295027 |
0.582253 |
-1.155263 |
0.112163 |
股票006 |
0.083849 |
0.638962 |
0.031900 |
-0.175389 |
-0.060951 |
股票007 |
0.089297 |
-1.544269 |
0.222476 |
-0.318276 |
1.456972 |
股票008 |
-1.316049 |
-1.092196 |
-0.641186 |
-0.551743 |
2.549434 |
股票009 |
-0.758271 |
0.195819 |
-0.118566 |
0.449031 |
-0.078919 |
stock_change.reset_index(drop=True)
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
|
2020-06-29 00:00:00 |
2020-06-30 00:00:00 |
2020-07-01 00:00:00 |
2020-07-02 00:00:00 |
2020-07-03 00:00:00 |
0 |
1.765488 |
-2.103328 |
-0.447653 |
1.613368 |
0.564403 |
1 |
-0.284062 |
0.331903 |
0.156971 |
-0.153528 |
0.391681 |
2 |
0.978045 |
0.702478 |
0.488523 |
-0.844833 |
-0.490149 |
3 |
0.326638 |
-1.184915 |
-1.856177 |
-0.083343 |
-0.884675 |
4 |
-0.060648 |
-0.328217 |
0.134691 |
-0.336444 |
0.492292 |
5 |
-0.089715 |
0.295027 |
0.582253 |
-1.155263 |
0.112163 |
6 |
0.083849 |
0.638962 |
0.031900 |
-0.175389 |
-0.060951 |
7 |
0.089297 |
-1.544269 |
0.222476 |
-0.318276 |
1.456972 |
8 |
-1.316049 |
-1.092196 |
-0.641186 |
-0.551743 |
2.549434 |
9 |
-0.758271 |
0.195819 |
-0.118566 |
0.449031 |
-0.078919 |
stock_change
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
|
2020-06-29 00:00:00 |
2020-06-30 00:00:00 |
2020-07-01 00:00:00 |
2020-07-02 00:00:00 |
2020-07-03 00:00:00 |
股票000 |
1.765488 |
-2.103328 |
-0.447653 |
1.613368 |
0.564403 |
股票001 |
-0.284062 |
0.331903 |
0.156971 |
-0.153528 |
0.391681 |
股票002 |
0.978045 |
0.702478 |
0.488523 |
-0.844833 |
-0.490149 |
股票003 |
0.326638 |
-1.184915 |
-1.856177 |
-0.083343 |
-0.884675 |
股票004 |
-0.060648 |
-0.328217 |
0.134691 |
-0.336444 |
0.492292 |
股票005 |
-0.089715 |
0.295027 |
0.582253 |
-1.155263 |
0.112163 |
股票006 |
0.083849 |
0.638962 |
0.031900 |
-0.175389 |
-0.060951 |
股票007 |
0.089297 |
-1.544269 |
0.222476 |
-0.318276 |
1.456972 |
股票008 |
-1.316049 |
-1.092196 |
-0.641186 |
-0.551743 |
2.549434 |
股票009 |
-0.758271 |
0.195819 |
-0.118566 |
0.449031 |
-0.078919 |
stock_change.set_index("2020-6-29 00:00:00")
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
|
2020-06-30 00:00:00 |
2020-07-01 00:00:00 |
2020-07-02 00:00:00 |
2020-07-03 00:00:00 |
2020-6-29 00:00:00 |
|
|
|
|
--- |
--- |
--- |
--- |
--- |
1.765488 |
-2.103328 |
-0.447653 |
1.613368 |
0.564403 |
-0.284062 |
0.331903 |
0.156971 |
-0.153528 |
0.391681 |
0.978045 |
0.702478 |
0.488523 |
-0.844833 |
-0.490149 |
0.326638 |
-1.184915 |
-1.856177 |
-0.083343 |
-0.884675 |
-0.060648 |
-0.328217 |
0.134691 |
-0.336444 |
0.492292 |
-0.089715 |
0.295027 |
0.582253 |
-1.155263 |
0.112163 |
0.083849 |
0.638962 |
0.031900 |
-0.175389 |
-0.060951 |
0.089297 |
-1.544269 |
0.222476 |
-0.318276 |
1.456972 |
-1.316049 |
-1.092196 |
-0.641186 |
-0.551743 |
2.549434 |
-0.758271 |
0.195819 |
-0.118566 |
0.449031 |
-0.078919 |
stock_change
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
|
2020-06-29 00:00:00 |
2020-06-30 00:00:00 |
2020-07-01 00:00:00 |
2020-07-02 00:00:00 |
2020-07-03 00:00:00 |
股票000 |
1.765488 |
-2.103328 |
-0.447653 |
1.613368 |
0.564403 |
股票001 |
-0.284062 |
0.331903 |
0.156971 |
-0.153528 |
0.391681 |
股票002 |
0.978045 |
0.702478 |
0.488523 |
-0.844833 |
-0.490149 |
股票003 |
0.326638 |
-1.184915 |
-1.856177 |
-0.083343 |
-0.884675 |
股票004 |
-0.060648 |
-0.328217 |
0.134691 |
-0.336444 |
0.492292 |
股票005 |
-0.089715 |
0.295027 |
0.582253 |
-1.155263 |
0.112163 |
股票006 |
0.083849 |
0.638962 |
0.031900 |
-0.175389 |
-0.060951 |
股票007 |
0.089297 |
-1.544269 |
0.222476 |
-0.318276 |
1.456972 |
股票008 |
-1.316049 |
-1.092196 |
-0.641186 |
-0.551743 |
2.549434 |
股票009 |
-0.758271 |
0.195819 |
-0.118566 |
0.449031 |
-0.078919 |
type(stock_change.loc["股票004"])
pandas.core.series.Series
stock_change.loc["股票004"]
2020-06-29 -0.060648
2020-06-30 -0.328217
2020-07-01 0.134691
2020-07-02 -0.336444
2020-07-03 0.492292
Freq: B, Name: 股票004, dtype: float64
pd.Series(np.arange(2,20,2))
0 2
1 4
2 6
3 8
4 10
5 12
6 14
7 16
8 18
dtype: int64
sr=pd.Series({'red':100,"blue":200,"queen":500, 'yellow':1000})
sr
blue 200
queen 500
red 100
yellow 1000
dtype: int64
sr.index
Index(['blue', 'queen', 'red', 'yellow'], dtype='object')
sr.values
array([ 200, 500, 100, 1000])
pandas 操作suoyin
data=pd.read_csv("./stock_day/stock_day.csv")
data = data.drop(["ma5","ma10","ma20","v_ma5","v_ma10","v_ma20"], axis=1) #让数据简单一些
data.head()
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
|
open |
high |
close |
low |
volume |
price_change |
p_change |
turnover |
2018-02-27 |
23.53 |
25.88 |
24.16 |
23.53 |
95578.03 |
0.63 |
2.68 |
2.39 |
2018-02-26 |
22.80 |
23.78 |
23.53 |
22.80 |
60985.11 |
0.69 |
3.02 |
1.53 |
2018-02-23 |
22.88 |
23.37 |
22.82 |
22.71 |
52914.01 |
0.54 |
2.42 |
1.32 |
2018-02-22 |
22.25 |
22.76 |
22.28 |
22.02 |
36105.01 |
0.36 |
1.64 |
0.90 |
2018-02-14 |
21.49 |
21.99 |
21.92 |
21.48 |
23331.04 |
0.44 |
2.05 |
0.58 |
data["open"]["2018-02-23"] #pandas必须先列后行
22.88
data.loc["2018-02-23"]["open"] #使用loc可以先行后列
22.88
data.loc["2018-02-26","open"]
22.8
data.iloc[1,0]
22.8
data.loc[data.index[0:4],['open','close','high','low']]
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
|
open |
close |
high |
low |
2018-02-27 |
23.53 |
24.16 |
25.88 |
23.53 |
2018-02-26 |
22.80 |
23.53 |
23.78 |
22.80 |
2018-02-23 |
22.88 |
22.82 |
23.37 |
22.71 |
2018-02-22 |
22.25 |
22.28 |
22.76 |
22.02 |
data.iloc[0:4,data.columns.get_indexer(['open','close','high','low'])]
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
|
open |
close |
high |
low |
2018-02-27 |
23.53 |
24.16 |
25.88 |
23.53 |
2018-02-26 |
22.80 |
23.53 |
23.78 |
22.80 |
2018-02-23 |
22.88 |
22.82 |
23.37 |
22.71 |
2018-02-22 |
22.25 |
22.28 |
22.76 |
22.02 |
data.price_change.head()
2018-02-27 0.63
2018-02-26 0.69
2018-02-23 0.54
2018-02-22 0.36
2018-02-14 0.44
Name: price_change, dtype: float64
# data["open"]=8
data.head()
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
|
open |
high |
close |
low |
volume |
price_change |
p_change |
turnover |
2018-02-27 |
23.53 |
25.88 |
24.16 |
23.53 |
95578.03 |
0.63 |
2.68 |
2.39 |
2018-02-26 |
22.80 |
23.78 |
23.53 |
22.80 |
60985.11 |
0.69 |
3.02 |
1.53 |
2018-02-23 |
22.88 |
23.37 |
22.82 |
22.71 |
52914.01 |
0.54 |
2.42 |
1.32 |
2018-02-22 |
22.25 |
22.76 |
22.28 |
22.02 |
36105.01 |
0.36 |
1.64 |
0.90 |
2018-02-14 |
21.49 |
21.99 |
21.92 |
21.48 |
23331.04 |
0.44 |
2.05 |
0.58 |
data=data.sort_values(by="p_change", ascending=False).head()
data.head()
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
|
open |
high |
close |
low |
volume |
price_change |
p_change |
turnover |
2015-08-28 |
15.40 |
16.46 |
16.46 |
15.00 |
117827.60 |
1.50 |
10.03 |
4.03 |
2015-05-21 |
27.50 |
28.22 |
28.22 |
26.50 |
121190.11 |
2.57 |
10.02 |
4.15 |
2016-12-22 |
18.50 |
20.42 |
20.42 |
18.45 |
150470.83 |
1.86 |
10.02 |
3.77 |
2015-08-04 |
16.20 |
17.35 |
17.35 |
15.80 |
94292.63 |
1.58 |
10.02 |
3.23 |
2016-07-07 |
18.66 |
18.66 |
18.66 |
18.41 |
48756.55 |
1.70 |
10.02 |
1.67 |
data=data.sort_values(by=['open','high'])
data.head()
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
|
open |
high |
close |
low |
volume |
price_change |
p_change |
turnover |
2015-08-28 |
15.40 |
16.46 |
16.46 |
15.00 |
117827.60 |
1.50 |
10.03 |
4.03 |
2015-08-04 |
16.20 |
17.35 |
17.35 |
15.80 |
94292.63 |
1.58 |
10.02 |
3.23 |
2016-12-22 |
18.50 |
20.42 |
20.42 |
18.45 |
150470.83 |
1.86 |
10.02 |
3.77 |
2016-07-07 |
18.66 |
18.66 |
18.66 |
18.41 |
48756.55 |
1.70 |
10.02 |
1.67 |
2015-05-21 |
27.50 |
28.22 |
28.22 |
26.50 |
121190.11 |
2.57 |
10.02 |
4.15 |
data.sort_index()
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
|
open |
high |
close |
low |
volume |
price_change |
p_change |
turnover |
2015-05-21 |
27.50 |
28.22 |
28.22 |
26.50 |
121190.11 |
2.57 |
10.02 |
4.15 |
2015-08-04 |
16.20 |
17.35 |
17.35 |
15.80 |
94292.63 |
1.58 |
10.02 |
3.23 |
2015-08-28 |
15.40 |
16.46 |
16.46 |
15.00 |
117827.60 |
1.50 |
10.03 |
4.03 |
2016-07-07 |
18.66 |
18.66 |
18.66 |
18.41 |
48756.55 |
1.70 |
10.02 |
1.67 |
2016-12-22 |
18.50 |
20.42 |
20.42 |
18.45 |
150470.83 |
1.86 |
10.02 |
3.77 |
data["p_change"].sort_values(ascending=True)
2015-08-04 10.02
2016-12-22 10.02
2016-07-07 10.02
2015-05-21 10.02
2015-08-28 10.03
Name: p_change, dtype: float64
data["p_change"].sort_index(ascending=True)
2015-05-21 10.02
2015-08-04 10.02
2015-08-28 10.03
2016-07-07 10.02
2016-12-22 10.02
Name: p_change, dtype: float64
data
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
|
open |
high |
close |
low |
volume |
price_change |
p_change |
turnover |
2015-08-28 |
15.40 |
16.46 |
16.46 |
15.00 |
117827.60 |
1.50 |
10.03 |
4.03 |
2015-08-04 |
16.20 |
17.35 |
17.35 |
15.80 |
94292.63 |
1.58 |
10.02 |
3.23 |
2016-12-22 |
18.50 |
20.42 |
20.42 |
18.45 |
150470.83 |
1.86 |
10.02 |
3.77 |
2016-07-07 |
18.66 |
18.66 |
18.66 |
18.41 |
48756.55 |
1.70 |
10.02 |
1.67 |
2015-05-21 |
27.50 |
28.22 |
28.22 |
26.50 |
121190.11 |
2.57 |
10.02 |
4.15 |
data+100
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
|
open |
high |
close |
low |
volume |
price_change |
p_change |
turnover |
2015-08-28 |
115.40 |
116.46 |
116.46 |
115.00 |
117927.60 |
101.50 |
110.03 |
104.03 |
2015-08-04 |
116.20 |
117.35 |
117.35 |
115.80 |
94392.63 |
101.58 |
110.02 |
103.23 |
2016-12-22 |
118.50 |
120.42 |
120.42 |
118.45 |
150570.83 |
101.86 |
110.02 |
103.77 |
2016-07-07 |
118.66 |
118.66 |
118.66 |
118.41 |
48856.55 |
101.70 |
110.02 |
101.67 |
2015-05-21 |
127.50 |
128.22 |
128.22 |
126.50 |
121290.11 |
102.57 |
110.02 |
104.15 |
data/100
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
|
open |
high |
close |
low |
volume |
price_change |
p_change |
turnover |
2015-08-28 |
0.1540 |
0.1646 |
0.1646 |
0.1500 |
1178.2760 |
0.0150 |
0.1003 |
0.0403 |
2015-08-04 |
0.1620 |
0.1735 |
0.1735 |
0.1580 |
942.9263 |
0.0158 |
0.1002 |
0.0323 |
2016-12-22 |
0.1850 |
0.2042 |
0.2042 |
0.1845 |
1504.7083 |
0.0186 |
0.1002 |
0.0377 |
2016-07-07 |
0.1866 |
0.1866 |
0.1866 |
0.1841 |
487.5655 |
0.0170 |
0.1002 |
0.0167 |
2015-05-21 |
0.2750 |
0.2822 |
0.2822 |
0.2650 |
1211.9011 |
0.0257 |
0.1002 |
0.0415 |
data.add(100)
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
|
open |
high |
close |
low |
volume |
price_change |
p_change |
turnover |
2015-08-28 |
115.40 |
116.46 |
116.46 |
115.00 |
117927.60 |
101.50 |
110.03 |
104.03 |
2015-08-04 |
116.20 |
117.35 |
117.35 |
115.80 |
94392.63 |
101.58 |
110.02 |
103.23 |
2016-12-22 |
118.50 |
120.42 |
120.42 |
118.45 |
150570.83 |
101.86 |
110.02 |
103.77 |
2016-07-07 |
118.66 |
118.66 |
118.66 |
118.41 |
48856.55 |
101.70 |
110.02 |
101.67 |
2015-05-21 |
127.50 |
128.22 |
128.22 |
126.50 |
121290.11 |
102.57 |
110.02 |
104.15 |
data["p_change"]>2
2015-08-28 True
2015-08-04 True
2016-12-22 True
2016-07-07 True
2015-05-21 True
Name: p_change, dtype: bool
data
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
|
open |
high |
close |
low |
volume |
price_change |
p_change |
turnover |
2015-08-28 |
15.40 |
16.46 |
16.46 |
15.00 |
117827.60 |
1.50 |
10.03 |
4.03 |
2015-08-04 |
16.20 |
17.35 |
17.35 |
15.80 |
94292.63 |
1.58 |
10.02 |
3.23 |
2016-12-22 |
18.50 |
20.42 |
20.42 |
18.45 |
150470.83 |
1.86 |
10.02 |
3.77 |
2016-07-07 |
18.66 |
18.66 |
18.66 |
18.41 |
48756.55 |
1.70 |
10.02 |
1.67 |
2015-05-21 |
27.50 |
28.22 |
28.22 |
26.50 |
121190.11 |
2.57 |
10.02 |
4.15 |
data[data["open"]>17]
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
|
open |
high |
close |
low |
volume |
price_change |
p_change |
turnover |
2016-12-22 |
18.50 |
20.42 |
20.42 |
18.45 |
150470.83 |
1.86 |
10.02 |
3.77 |
2016-07-07 |
18.66 |
18.66 |
18.66 |
18.41 |
48756.55 |
1.70 |
10.02 |
1.67 |
2015-05-21 |
27.50 |
28.22 |
28.22 |
26.50 |
121190.11 |
2.57 |
10.02 |
4.15 |
data.query("p_change>10&open>18").head()
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
|
open |
high |
close |
low |
volume |
price_change |
p_change |
turnover |
2016-12-22 |
18.50 |
20.42 |
20.42 |
18.45 |
150470.83 |
1.86 |
10.02 |
3.77 |
2016-07-07 |
18.66 |
18.66 |
18.66 |
18.41 |
48756.55 |
1.70 |
10.02 |
1.67 |
2015-05-21 |
27.50 |
28.22 |
28.22 |
26.50 |
121190.11 |
2.57 |
10.02 |
4.15 |
data["price_change"].isin([1.86,2.57])
2015-08-28 False
2015-08-04 False
2016-12-22 True
2016-07-07 False
2015-05-21 True
Name: price_change, dtype: bool
data.describe()
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
|
open |
high |
close |
low |
volume |
price_change |
p_change |
turnover |
count |
5.000000 |
5.000000 |
5.000000 |
5.000000 |
5.000000 |
5.000000 |
5.000000 |
5.000000 |
mean |
19.252000 |
20.222000 |
20.222000 |
18.832000 |
106507.544000 |
1.842000 |
10.022000 |
3.370000 |
std |
4.824367 |
4.712963 |
4.712963 |
4.555203 |
37950.208695 |
0.428976 |
0.004472 |
1.014101 |
min |
15.400000 |
16.460000 |
16.460000 |
15.000000 |
48756.550000 |
1.500000 |
10.020000 |
1.670000 |
25% |
16.200000 |
17.350000 |
17.350000 |
15.800000 |
94292.630000 |
1.580000 |
10.020000 |
3.230000 |
50% |
18.500000 |
18.660000 |
18.660000 |
18.410000 |
117827.600000 |
1.700000 |
10.020000 |
3.770000 |
75% |
18.660000 |
20.420000 |
20.420000 |
18.450000 |
121190.110000 |
1.860000 |
10.020000 |
4.030000 |
max |
27.500000 |
28.220000 |
28.220000 |
26.500000 |
150470.830000 |
2.570000 |
10.030000 |
4.150000 |
data.idxmax()
open 2015-05-21
high 2015-05-21
close 2015-05-21
low 2015-05-21
volume 2016-12-22
price_change 2015-05-21
p_change 2015-08-28
turnover 2015-05-21
dtype: object
data=pd.read_csv("./stock_day/stock_day.csv")
data = data.drop(["ma5","ma10","ma20","v_ma5","v_ma10","v_ma20"], axis=1) #让数据简单一些
data.head()
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
|
open |
high |
close |
low |
volume |
price_change |
p_change |
turnover |
2018-02-27 |
23.53 |
25.88 |
24.16 |
23.53 |
95578.03 |
0.63 |
2.68 |
2.39 |
2018-02-26 |
22.80 |
23.78 |
23.53 |
22.80 |
60985.11 |
0.69 |
3.02 |
1.53 |
2018-02-23 |
22.88 |
23.37 |
22.82 |
22.71 |
52914.01 |
0.54 |
2.42 |
1.32 |
2018-02-22 |
22.25 |
22.76 |
22.28 |
22.02 |
36105.01 |
0.36 |
1.64 |
0.90 |
2018-02-14 |
21.49 |
21.99 |
21.92 |
21.48 |
23331.04 |
0.44 |
2.05 |
0.58 |
data["p_change"].sort_index().cumsum().head()
2015-03-02 2.62
2015-03-03 4.06
2015-03-04 5.63
2015-03-05 7.65
2015-03-06 16.16
Name: p_change, dtype: float64
data["p_change"].sort_index().cumsum().plot()
<matplotlib.axes._subplots.AxesSubplot at 0x7f6c86b3c9e8>