【略】
二、数据初步探索
2.1数据展示:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = pd.read_csv('indest20210114.csv')
#删除最后一行,通达信数据声明
df.drop([len(df) - 1], inplace=True)
#数据信息和数据格式
print(df.info())
#数据因子
print(df.columns)
#数据前五列
print(df.head())
#数据后五列
print(df.tail())
数据格式展示:
0 代码 56 non-null object
1 名称 56 non-null object
2 涨幅% 56 non-null float64
3 现价 56 non-null float64
4 涨跌 56 non-null float64
5 涨速% 56 non-null float64
6 量比 56 non-null float64
7 涨跌数 56 non-null object
8 涨停数 56 non-null float64
9 总金额 56 non-null float64
10 开盘金额 56 non-null float64
11 换手% 56 non-null float64
12 连涨天 56 non-null float64
13 3日涨幅% 56 non-null float64
14 20日涨幅% 56 non-null float64
15 60日涨幅% 56 non-null float64
16 年初至今% 56 non-null float64
17 净流入 56 non-null float64
18 强弱度% 56 non-null float64
19 总量 56 non-null float64
20 市盈(动) 56 non-null object
21 市净率 56 non-null float64
22 振幅% 56 non-null float64
23 昨收 56 non-null float64
24 今开 56 non-null float64
25 最高 56 non-null float64
26 最低 56 non-null float64
27 均价 56 non-null float64
28 开盘% 56 non-null float64
29 现均差% 56 non-null float64
30 流通市值 56 non-null object
31 AB股总市值 56 non-null object
32 流通股(亿) 56 non-null float64
33 总股本(亿) 56 non-null float64
34 短期形态 0 non-null float64
35 中期形态 0 non-null float64
36 长期形态 0 non-null float64
通过查询我们可以得知df数据【短期、中期、长期】形态三个因子为空,进行删除,其次df数据中【‘涨跌数’】、【‘市盈(动)’】、【‘流通市值’】、【‘AB股总市值’】为字符串格式,因此我们必须对这四个因子进行数据处理。
涨跌数 市盈(动) 流通市值 AB股总市值
0 23,2 21.65 3123.01亿 3797.61亿
1 7,1 108.21 319.56亿 323.27亿
2 9,0 27.54 2303.67亿 3101.52亿
3 43,14 15.25 7353.03亿 9330.65亿
4 29,11 31.36 1700.94亿 2101.88亿
我们发现【‘涨跌数’】中是包含涨和跌两个数值,并且由逗号隔开,【‘市盈(动)’】中含有‘– ’的字符串格式,【‘流通市值’】、【‘AB股总市值’】中是数值+字符串的形式构成的字符串,现在一一进行处理:
#1.对于【‘涨跌数’】的处理思路:
#通过推导式将涨跌幅数据隔离开
df['涨幅数']=[i.split(',')[0] for i in df['涨跌数']]
df['跌幅数']=[i.split(',')[1] for i in df['涨跌数']]
#2.对于【‘市盈(动)’】的处理思路:
#先将非空统计出来转格式
pe_mean=df[df['市盈(动)'] != '-- ']['市盈(动)'].astype(float)
#计算平均数并且保留两位小数
pe_mean=round(pe_mean.mean(),2)
#用平均值去替换,这里是采取机器学习中的一种填充方法
df['市盈(动)'].replace('-- ',pe_mean,inplace=True)
#3.对【‘流通市值’】、【‘AB股总市值’】的处理
#通过推导式和切片的方式取值
df['流通市值']=[i[:-1] for i in df['流通市值']]
df['AB股总市值']=[i[:-1] for i in df['AB股总市值']]
print(df[['涨幅数','跌幅数','市盈(动)','流通市值', 'AB股总市值']].head())
数据展示:
涨幅数 跌幅数 市盈(动) 流通市值 AB股总市值
0 23 2 21.65 3123.01 3797.61
1 7 1 108.21 319.56 323.27
2 9 0 27.54 2303.67 3101.52
3 43 14 15.25 7353.03 9330.65
4 29 11 31.36 1700.94 2101.88
考虑到我们对于数据处理是用于无监督学习中的聚类,必须对一些高维数据进行降维处理,才能放置在一个维度进行处理,不然很容易因为这些数值权重影响过大,导致分析失真,出现其他结果。因此我们这里仍然采取平均数进行处理。
list01=['现价','涨跌','总金额','开盘金额','总量','市盈(动)','均价','流通市值','AB股总市值','流通股(亿)','总股本(亿)']
for col in list01:
df[col]=df[col]/df[col].mean()
删除不必要的数据:
df01=df[['代码', '名称', '涨幅%', '现价', '涨跌', '涨速%', '量比', '涨停数', '总金额', '开盘金额',
'换手%', '连涨天', '3日涨幅%', '20日涨幅%', '60日涨幅%', '年初至今%', '强弱度%', '总量',
'市盈(动)', '市净率', '振幅%', '均价', '开盘%', '现均差%',
'流通市值', 'AB股总市值', '流通股(亿)', '总股本(亿)', '涨幅数',
'跌幅数']]
发布者:股市刺客,转载请注明出处:https://www.95sca.cn/archives/496114
站内所有文章皆来自网络转载或读者投稿,请勿用于商业用途。如有侵权、不妥之处,请联系站长并出示版权证明以便删除。敬请谅解!