2018江西省数学建模A题:第一问中关于按季度统计数据的一种解决办法。
即本文解决的问题是:把原始数据中得过敏性疾病的患者数据提取出来,并按照每年的四季分类,最终得出分类数据,并画出按四季分类的直方图。
1、从excel读取数据:
import numpy as np
import pandas as pd # 导入pandas库
file_directory = "2018年过敏统计信息表 A题附件.xls" #原始数据的文件名
data = pd.read_excel(file_directory,sep='\t',parse_dates=['检测日期']) #读取数据,并保存到data中
2、清洗数据:
# 根据观察的信息,清洗数据
# 清除无用的列
data.drop(['Unnamed: 21'], axis=1,inplace=True) #删除Unnamed: 21列(即最后一列)
# 清除无用的列
data_drop=data.dropna(axis=0, how='any') # Remove missing values. 删除有过敏原有空值的那一行
print(list(data)) # 标题 显示验证最后一列Unnamed被删除
print('清洗前数据行数:',len(data)) # 数据行数(2284)
print('数据清洗后长度:',len(data_drop)) # 数据长度(2267)
len(data)- len(data_drop)# 去掉无用的数据的行数(17)
(len(data)- len(data_drop))/len(data)*100 # (处理掉的数据占0.744%)
3、数据类型数字化:
# 定义一个等级替换函数
# 过敏分级:0-6(共7级)
# [ 0 1 2 3 4 5 6]
# ['0(-)' '1(±)' '2(+)' '3(2+)' '4(3+)' '5(4+)' '6(5+)']
def Allergic_classification_str(header):
data_drop[header] = data_drop.loc[:,header].replace(['0(-)','1(±)','2(+)','3(2+)','4(3+)',
'5(4+)','6(5+)'],[0,1,2,3,4,5,6])
l=range(len(list(data_drop)))
ll=l[4:] #从第四列开始到最后一列都是过敏原
i=4
for i in ll:
Allergic_classification_str(list(data_drop)[i]) #调用等级替换函数,把字符替换成相应等级的数字
4、给每行数据添加相应的标签:
按是否为过敏性疾病患者添加标签
#在数据的最后添加一行label 0代表没有病,1代表有病
data_drop_label = data_drop.join(data_label.label)
data_drop_label.head(5) # 查看添加标签后的数据前5行
按季度添加标签
# 按时间提取数据
# 设置一个dataframe格式的数据,以便存放季节标签
season_label = pd.DataFrame({'season':np.zeros(data_drop.shape[0])})
# 把数据按每年的季节分为20个标签
for i in data_drop.index:
if (data_drop.检测日期[i].month>=3) & (data_drop.检测日期[i].month<6):
season_label['season'][i]=str(data_drop.检测日期[i].year)+'spring'
if (data_drop.检测日期[i].month>=6) & (data_drop.检测日期[i].month<9):
season_label['season'][i]=str(data_drop.检测日期[i].year)+'summer'
if (data_drop.检测日期[i].month>=9) & (data_drop.检测日期[i].month<12):
season_label['season'][i]=str(data_drop.检测日期[i].year)+'autumn'
if (data_drop.检测日期[i].month>=12) | (data_drop.检测日期[i].month<3):
season_label['season'][i]=str(data_drop.检测日期[i].year)+'winter'
data_s_t=data_drop_label.join(season_label.season)#在数据的最后添加一行season 代表一年四季
# 到现在data_s_t数据中即有label标签页有代表季节标签
5、按添加的标签的统计过敏性患者各个季度的数量
data_allergy_s = data_s_t[data_s_t.label.isin([1])] #提取出有过敏性疾病的数据
# 建立了季度与疾病就诊人数的联系
S =['2013spring','2014spring','2015spring','2016spring','2017spring',
'2013summer','2014summer','2015summer','2016summer','2017summer',
'2013autumn','2014autumn','2015autumn','2016autumn','2017autumn',
'2013winter','2014winter','2015winter','2016winter','2017winter']
S_count=np.zeros(len(S))
for j in data_allergy_s.index:
for i in range(len(S)):
if data_allergy_s.season[j]==S[i]:
S_count[i] = S_count[i]+1
6、画图
# 画出按季节分布的图
import matplotlib.pyplot as plt
%matplotlib inline
# plt.bar(range(len(S_count)),S_count,width=1.5,color = 'c',label = 'm=2')
plt.bar([0,1,2,3,4],[S_count[0],S_count[1],S_count[2],S_count[3],S_count[4]],color = 'g',label = 'm=2')
plt.bar([5,6,7,8,9],[S_count[5],S_count[6],S_count[7],S_count[8],S_count[9]],color = 'r',label = 'm=2')
plt.bar([10,11,12,13,14],[S_count[10],S_count[11],S_count[12],S_count[13],S_count[14]],color = 'y',label = 'm=2')
plt.bar([15,16,17,18,19],[S_count[15],S_count[16],S_count[17],S_count[18],S_count[19]],color = 'b',label = 'm=2')
for x,y,z in zip(range(len(S_count)),S_count,S):
plt.text(x+0.3,y,'%d'%y,ha='center',va='bottom')
t=plt.text(x+0.4,-35,'%s'%z,ha='center',va='bottom',fontproperties ='SimHei', fontsize=13)
t.set_rotation(90)
# plt.xlabel('季节',fontproperties ='SimHei', fontsize=15)
plt.ylabel('过敏性疾病人数',fontproperties ='SimHei', fontsize=20)
plt.xticks([])
plt.grid()
plt.figure(figsize=(60,20))
至此问题解决。
全部的数据和程序见:https://github.com/minda163/math_modelgithub.com
如有错误,欢迎指教!
本文介绍了如何使用Python从Excel数据中提取过敏性疾病患者的季度统计数据。通过读取数据,清洗,将过敏等级数字化,添加疾病和季度标签,统计每个季度的患者数量并绘制直方图。

900

被折叠的 条评论
为什么被折叠?



