如果有任何证据表明疫苗的有效性与儿童的性别之间存在联系,这将是很有趣的。计算感染水痘但接种了疫苗(至少一剂水痘疫苗)的儿童人数与接种了疫苗但没有感染水痘的儿童人数的比率。按性别返回结果。此函数应返回以下形式的字典(使用正确的数字):
{"male":0.2, "female":0.4}
注:为了帮助验证,自动分级机正在查找的水痘_by_sex()['女性']值以数字0.0077开头。请编写相同的功能代码。
t1qtbnec1#
使用以下代码读取给定的数据集
import pandas as pd df=pd.read_csv('assets/NISPUF17.csv',index_col=0) df
主代码
def chickenpox_by_sex(): # YOUR CODE HERE male_df=df[df['SEX']==1] vac_m=male_df[male_df['P_NUMVRC']>=1] cp_m=vac_m[vac_m['HAD_CPOX']==1] counts_cp_m=cp_m['SEX'].count() ncp_m=vac_m[vac_m['HAD_CPOX']==2] counts_ncp_m=ncp_m['SEX'].count() male=counts_cp_m/counts_ncp_m female_df=df[df['SEX']==2] vac_f=female_df[female_df['P_NUMVRC']>=1] cp_f=vac_f[vac_f['HAD_CPOX']==1] counts_cp_f=cp_f['SEX'].count() ncp_f=vac_f[vac_f['HAD_CPOX']==2] counts_ncp_f=ncp_f['SEX'].count() female=counts_cp_f/counts_ncp_f ratio_dict={"male":male,"female":female} return ratio_dict raise NotImplementedError()
使用以下代码进行检查
chickenpox_by_sex()['female']
完成此操作的最终代码
assert len(chickenpox_by_sex())==2, "Return a dictionary with two items, the first for males and the second for females."
=〉[性别]-〉性别= 1(雄性);性别= 2(雌性)=〉[HAD_COP]-〉感染水痘= 1;未感染水痘= 2=〉[P_NUMVRC]〉= 1-〉给予一剂或多剂
变量名称:公-公 Dataframevac_m-接种雄性cp_m-接种和感染水痘(雄性)counts_cp_m-接种和感染水痘的计数ncp_m-接种疫苗但未感染水痘(男性)counts_ncp_m-接种疫苗且未感染水痘女性也是如此。
qacovj5a2#
正确的解决方案。
def chickenpox_by_sex(): import pandas as pd df = pd.read_csv("NISPUF17.csv") maleDf = df[df["SEX"] ==1] doses1 = maleDf[maleDf["P_NUMVRC"] >= 1] chichkenPox1_1 = doses1[doses1["HAD_CPOX"] == 1] count1_1 = chichkenPox1_1["SEX"].count() chichkenPox1_2 = doses1[doses1["HAD_CPOX"] == 2] count1_2 = chichkenPox1_2["SEX"].count() resultMale = count1_1/count1_2 femaleDf = df[df["SEX"] == 2] doses2 = femaleDf[femaleDf["P_NUMVRC"] >= 1] chichkenPox2_1 = doses2[doses2["HAD_CPOX"] == 1] count2_1 = chichkenPox2_1["SEX"].count() chichkenPox2_2 = doses2[doses2["HAD_CPOX"] == 2] count2_2 = chichkenPox2_2["SEX"].count() resultFemale = count2_1/count2_2 dict = {"male":resultMale, "female":resultFemale } return dict
jdzmm42g3#
下面的代码同样适用:
import pandas as pd import numpy as np import math def chickenpox_by_sex(): df=pd.read_csv('assets/NISPUF17.csv') c_vaccinated=df[df['P_NUMVRC']>0] menstats=c_vaccinated[c_vaccinated['SEX']==1] mnocpox=len(menstats[menstats['HAD_CPOX']==2]) menratio=len(menstats[menstats['HAD_CPOX']==1])/mnocpox wstats=c_vaccinated[c_vaccinated['SEX']==2] wnocpox=len(wstats[wstats['HAD_CPOX']==2]) wratio=len(wstats[wstats['HAD_CPOX']==1])/wnocpox ratios={'male':menratio,'female':wratio} return ratios chickenpox_by_sex()
1sbrub3j4#
import pandas as pd def chickenpox_by_sex(): df = pd.read_csv('assets/NISPUF17.csv') df = df.drop(df[df.HAD_CPOX == 77].index) df = df.drop(df[df.HAD_CPOX == 99].index) df = df.dropna(subset=['P_NUMVRC']) df.loc[df['HAD_CPOX'] == 1, 'HAD_CPOX'] = 'YES' df.loc[df['HAD_CPOX'] == 2, 'HAD_CPOX'] = 'NO' df.loc[df['SEX'] == 1, 'SEX'] = 'male' df.loc[df['SEX'] == 2, 'SEX'] = 'female' df.loc[df['P_NUMVRC'] == 2.0, 'P_NUMVRC'] = 1 df.loc[df['P_NUMVRC'] == 3.0, 'P_NUMVRC'] = 1 df = df[['SEX', 'P_NUMVRC', 'HAD_CPOX']].round(decimals=0) dfm = df[df['SEX'] == 'male'] dfmVac = dfm[dfm['P_NUMVRC'] == 1.0] mPoxVacYes = len(dfmVac[dfmVac['HAD_CPOX'] == 'YES']) mPoxVacNo = len(dfmVac[dfmVac['HAD_CPOX'] == 'NO']) dff = df[df['SEX'] == 'female'] dffVac = dff[dff['P_NUMVRC'] == 1.0] fPoxVacYes = len(dffVac[dffVac['HAD_CPOX'] == 'YES']) fPoxVacNo = len(dffVac[dffVac['HAD_CPOX'] == 'NO']) ratioM = mPoxVacYes/float(mPoxVacNo) ratioF = fPoxVacYes/float(fPoxVacNo) result = {'male': ratioM * 100, 'female': ratioF * 100} return result
beq87vna5#
import pandas as pd import numpy as np df = pd.read_csv('assets/NISPUF17.csv', usecols = ['HAD_CPOX', 'SEX', 'P_NUMVRC']).dropna().reset_index() def chickenpox_by_sex(): girls = df[df.SEX == 2] girls_had = girls[(girls.HAD_CPOX == 1) & (girls.P_NUMVRC > 0.0)] girls_not_had = girls[(girls.HAD_CPOX == 2) &(girls.P_NUMVRC > 0.0)] girls_ratio = len(girls_had)/len(girls_not_had) boys = df[df.SEX == 1] boys_had = boys[(boys.HAD_CPOX == 1) & (boys.P_NUMVRC > 0.0)] boys_not_had = boys[(boys.HAD_CPOX == 2) &(boys.P_NUMVRC > 0.0)] boys_ratio = len(boys_had)/len(boys_not_had) result = {"male": round(boys_ratio, ndigits=4), "female":round(girls_ratio, ndigits = 4)} return result chickenpox_by_sex()
5条答案
按热度按时间t1qtbnec1#
尝试以下代码:
使用以下代码读取给定的数据集
主代码
使用以下代码进行检查
完成此操作的最终代码
=〉[性别]-〉性别= 1(雄性);性别= 2(雌性)
=〉[HAD_COP]-〉感染水痘= 1;未感染水痘= 2
=〉[P_NUMVRC]〉= 1-〉给予一剂或多剂
变量名称:
公-公 Dataframe
vac_m-接种雄性
cp_m-接种和感染水痘(雄性)
counts_cp_m-接种和感染水痘的计数
ncp_m-接种疫苗但未感染水痘(男性)
counts_ncp_m-接种疫苗且未感染水痘
女性也是如此。
qacovj5a2#
正确的解决方案。
jdzmm42g3#
下面的代码同样适用:
1sbrub3j4#
beq87vna5#