为巩固所学的pandas、seaborn、matplotlib等包,作此练习。
根据在kaggle中下载的数据,本文将分析怎么样的饮食习惯能够帮助抵抗新型冠状病毒。
数据来源:kaggle
1.导入模块和数据
# 导入所需模块
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
plt.style.use('ggplot')
# 导入数据
fatsq = pd.read_csv('fatsq.csv')
fsk = pd.read_csv('fsk.csv')
fsqkg = pd.read_csv('fsqkg.csv')
psq = pd.read_csv('psq.csv')
sfd = pd.read_csv('sfd.csv')
2.检查和清洗数据
# 查看数据
fatsq.info()
fatsq.head(20)
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 170 entries, 0 to 169
Data columns (total 32 columns):
Country 170 non-null object
Alcoholic Beverages 170 non-null float64
Animal Products 170 non-null float64
Animal fats 170 non-null float64
Aquatic Products, Other 170 non-null float64
Cereals - Excluding Beer 170 non-null float64
Eggs 170 non-null float64
Fish, Seafood 170 non-null float64
Fruits - Excluding Wine 170 non-null float64
Meat 170 non-null float64
Miscellaneous 170 non-null float64
Milk - Excluding Butter 170 non-null float64
Offals 170 non-null float64
Oilcrops 170 non-null float64
Pulses 170 non-null float64
Spices 170 non-null float64
Starchy Roots 170 non-null float64
Stimulants 170 non-null float64
Sugar Crops 170 non-null float64
Sugar & Sweeteners 170 non-null float64
Treenuts 170 non-null float64
Vegetal Products 170 non-null float64
Vegetable Oils 170 non-null float64
Vegetables 170 non-null float64
Obesity 167 non-null float64
Undernourished 163 non-null object
Confirmed 161 non-null float64
Deaths 161 non-null float64
Recovered 161 non-null float64
Active 161 non-null float64
Population 170 non-null float64
Unit (all except Population) 170 non-null object
dtypes: float64(29), object(3)
memory usage: 42.6+ KB
#fatsq
fatsq = fatsq.dropna(axis=0)
fatsq.Undernourished = fatsq.Undernourished.str.replace("<2.5","2")
fatsq.Undernourished = pd.to_numeric(fatsq.Undernourished,errors='coerce')
#fsk
fsk = fsk.dropna(axis=0)
fsk.Undernourished = fsk.Undernourished.str.replace("<2.5","2")
fsk.Undernourished = pd.to_numeric(fsk.Undernourished,errors='coerce')
#fsqkg
fsqkg = fsqkg.dropna(axis=0)
fsqkg.Undernourished = fsqkg.Undernourished.str.replace("<2.5","2")
fsqkg.Undernourished = pd.to_numeric(fsqkg.Undernourished,errors='coerce')
#psq
psq = psq.dropna(axis=0)
psq.Undernourished = psq.Undernourished.str.replace("<2.5","2")
psq.Undernourished = pd.to_numeric(psq.Undernourished,errors='coerce')
- 对于含空值的数据,若赋值为0,会对结果影响较大,故全部删去所在行
- 而Undernourished列的值应该都是数字,查看后发现异常值都是’<2.5’,故全改成’2’,并转换成float类型
- 后三个文件经观察是相同的问题,故作相同操作
- 操作检查后,数据已不存在其他问题
3.根据所知数据,首先探究肥胖、营养不良和新型冠状病毒的患病率、死亡率、治愈率的关系
# 图一 肥胖和确诊率
sns.lmplot('Obesity','Confirmed',data=fatsq)
plt.show()
由图可知,肥胖率高的国家,确诊率也高很多,而肥胖率低的国家,确诊率普遍很低,没有高的情况出现。
# 图二 肥胖和死亡率
sns.lmplot('Obesity','Deaths',data=fatsq)
plt.show()
由图可知,高死亡率出现在高肥胖率的国家
# 图三 营养不良和确诊率
sns.lmplot('Undernourished','Confirmed',data=fatsq)
plt.show()
# 图四 营养不良和死亡率
sns.lmplot('Undernourished','Deaths',data=fatsq)
plt.show()
- 而营养相对不足与国家确诊率和死亡率程负相关,与常识不符。分析原因可能是,这些国家富有,所以流动性高肥胖率高,故而可分析肥胖率和营养不良率之间的关系
sns.lmplot('Obesity','Undernourished',fatsq)
plt.show()
- 有图可见一般肥胖率高的国家,营养不良率就低,而营养不良率相对高的国家,肥胖率很低
由此可见,肥胖率对新型冠状病毒的影响较大,且成正相关,而营养不良的影响较低
y = [i for i in range(1,24)]
s1 = list(map(lambda x:fsqkg.iloc[:,x].corr(fsqkg.iloc[:,24]),y)) #求出每种食物脂肪摄取量与肥胖率的相关性系数
s2 = list(map(lambda x:fsqkg.iloc[:,x].corr(fsqkg.iloc[:,26]),y)) #求出每种食物脂肪摄取量与确诊率的相关性系数
s3 = list(map(lambda x:psq.iloc[:,x].corr(fsqkg.iloc[:,26]),y)) #求出每种食物蛋白质与确诊率的相关性系数
df1 = pd.DataFrame(s1,index=fsqkg.columns[1:24],columns=fsqkg.columns[24:25])
df2 = pd.DataFrame(s2,index=fsqkg.columns[1:24],columns=fsqkg.columns[26:27])
df3 = pd.DataFrame(s3,index=fsqkg.columns[1:24],columns=fsqkg.columns[26:27])
df1 = df1.sort_values('Obesity')
df2 = df2.sort_values('Confirmed')
df3 = df3.sort_values('Confirmed')
df1.plot.bar(figsize=(15,8))
plt.show()
由每种食物脂肪摄取量与肥胖率的相关性系数作图如上
df2.plot.bar(figsize=(15,8))
plt.show()
由每种食物脂肪摄取量与确诊率的相关性系数作图如上
df3.plot.bar(figsize=(15,8))
plt.show()
由每种食物蛋白质摄取量与肥胖率的相关性系数作图如上
#筛选出相关性高的食物类型
df11 = df1.query('Obesity>0.4')
df22 = df2.query('Confirmed>0.2')
df33 = df3.query('Confirmed<-0.2')
#和食物类型解释表相结合,得出和肥胖确诊率相关性的食物
cf = df11.join(df22,how='inner')
eat_no = cf.merge(sfd,left_on=cf.index,right_on=sfd.Categories)
eat_no
key_0 | Obesity | Confirmed | Categories | Items | |
---|---|---|---|---|---|
0 | Animal fats | 0.408394 | 0.401826 | Animal fats | Butter, Ghee; Cream; Fats, Animals, Raw; Fish,... |
1 | Eggs | 0.457353 | 0.325090 | Eggs | Eggs |
2 | Milk - Excluding Butter | 0.498707 | 0.372638 | Milk - Excluding Butter | Milk - Excluding Butter |
3 | Meat | 0.568245 | 0.327027 | Meat | Bovine Meat; Meat, Other; Mutton & Goat Meat; ... |
4 | Animal Products | 0.578788 | 0.463564 | Animal Products | Aquatic Animals, Others; Aquatic Plants; Bovin... |
上表为与确诊率和肥胖率相关性高得食物,平时最好减少摄取量
eat_yes = df33.merge(sfd,left_on=df33.index,right_on=sfd.Categories)
eat_yes
key_0 | Confirmed | Categories | Items | |
---|---|---|---|---|
0 | Treenuts | -0.515067 | Treenuts | Nuts and products |
1 | Cereals - Excluding Beer | -0.442146 | Cereals - Excluding Beer | Barley and products; Cereals, Other; Maize and... |
2 | Oilcrops | -0.282788 | Oilcrops | Coconuts - Incl Copra; Cottonseed; Groundnuts ... |
3 | Offals | -0.277227 | Offals | Offals, Edible |
上表为与确诊率和肥胖率负相关性高得食物,我们可以多从这些食物中摄取所需的营养物