# 深交所股票行情数据5.29-6.2,取5日平均涨幅最高的5只股票、5日平均成交量最大的3只股票
# 深交所股票行情数据5.29-6.9(5.29-6.2 6.5-6.9),取10日内单日价差(最高-最低)最大的2只股票
import pandas as pd
hq1 = pd.read_excel(r"D:\火狐下载\股票行情5月29至6月2.xlsx")
for i in range(hq1.shape[0]):
temp = hq1.loc[i,"成交量(万股)"]
temp = float(temp.replace(",","")) #用空白替换掉千位分隔符
hq1.loc[i,"成交量(万股)"] = temp #把新的赋值给旧的
hq11 = hq1.groupby(["证券代码","证券简称"]) #分组
hqzf = hq11["涨跌幅(%)"].mean() #zf=涨幅
hqzf = pd.DataFrame(hqzf)
hqzf.to_excel(r'D:\火狐下载\hqzf.xlsx') #先写入,如果要执行第二次请把第一次的Excel删除
hqzf = pd.read_excel(r"D:\火狐下载\hqzf.xlsx") #再读取,利用这个方法可以调整格式
hqzf.sort_values(by="涨跌幅(%)",ascending=False,inplace=True)
hqzf.index = range(hqzf.shape[0]) #重新设置行索引
zf_result = []
for i in range(hqzf.shape[0]):
if i == 5:
break
else:
zf_result.append(hqzf.loc[i,"证券简称"])
print("按涨跌幅取到的5只股票是:"+str(zf_result))
hqcjl = hq11["成交量(万股)"].mean() #cjl=成交量
hqcjl = pd.DataFrame(hqcjl)
hqcjl.to_excel(r'D:\火狐下载\hqcjl.xlsx')
hqcjl = pd.read_excel(r"D:\火狐下载\hqcjl.xlsx")
hqcjl.sort_values(by="成交量(万股)",ascending=False,inplace=True)
hqcjl.index = range(hqcjl.shape[0])
cjl_result = []
for i in range(hqcjl.shape[0]):
if i == 3:
break
else:
cjl_result.append(hqcjl.loc[i,"证券简称"])
print("按成交量取到的3只股票是:"+str(cjl_result))
hq2 = pd.read_excel(r"D:\火狐下载\股票行情6月5至6月9.xlsx")
hq3 = pd.concat([hq1,hq2]) #连接两个5日的数据
hq3["价差"] = hq3["最高"] - hq3["最低"]
hq3.sort_values(by="价差",ascending=False,inplace=True)
hq3.index = range(hq3.shape[0])
jc_result = [] #jc=价差
for i in range(hq3.shape[0]):
if len(jc_result) == 2:
break
else:
if hq3.loc[i,"证券代码"] == hq3.loc[i+1,"证券代码"]:
continue
else:
jc_result.append(hq3.loc[i, "证券简称"])
print("按价差取到的2只股票是:"+str(jc_result))
实训加分项
于 2023-07-08 11:59:41 首次发布