import pandas as pd
import numpy as np
# 读取Excel文件,pd.read_excel(r'文件位置\文件名称.xlsx')
hq = pd.read_excel(r'D:\火狐下载\股票行情20230406.xlsx') # hq=行情
print(hq)
print(hq.shape) # (2807, 13)
hq["涨跌幅"] = np.zeros((2807,1)) # 每种方法用完运行一次这段代码以重置数据
# 计算涨跌幅并填充
# 方法一:用while循环
i = 0
while i < hq.shape[0]: # hq.shape[0]=2807
hq.loc[i,"涨跌幅"] = round((hq.loc[i,"今收"] / hq.loc[i,"前收"] - 1) * 100, 2)
i += 1
print(hq)
# 方法二:用for循环
for i in range(0,hq.shape[0]):
hq.loc[i,"涨跌幅"] = round((hq.loc[i,"今收"] / hq.loc[i,"前收"] - 1) * 100, 2)
print(hq)
# 方法三:直接进行列运算
hq["涨跌幅"] = round((hq["今收"] / hq["前收"] - 1) * 100,2)
print(hq)
#成交量分析
import pandas as pd
import numpy as np
hq = pd.read_excel(r'D:\火狐下载\股票行情20230406.xlsx') # hq=行情
hq["成交量分析"] = np.zeros((hq.shape[0],1))
#用while
i = 0
while i < hq.shape[0]:
a = hq.iloc[i,9]
a = float(a.replace(",",""))
if a < 1000:
hq.loc[i,"成交量分析"] = "不活跃"
elif a < 2000:
hq.loc[i, "成交量分析"] = "一般"
else:
hq.loc[i, "成交量分析"] = "活跃"
i += 1
#用for
for i in range(hq.shape[0]):
a = hq.iloc[i,9]
a = float(a.replace(",",""))
if a < 1000:
hq.loc[i,"成交量分析"] = "不活跃"
elif a < 2000:
hq.loc[i, "成交量分析"] = "一般"
else:
hq.loc[i, "成交量分析"] = "活跃"
#统计各区间股票数量
#方法一
Small,Medium,Large = 0,0,0
for i in range(hq.shape[0]):
a = hq.iloc[i,9]
a = float(a.replace(",",""))
if a < 1000:
hq.loc[i,"成交量分析"] = "不活跃"
Small += 1
elif a < 2000:
hq.loc[i, "成交量分析"] = "一般"