Python 数据分析不需要任何东西
编写一个名为proportion_of_education的函数,返回数据集中母亲教育程度低于高中(12)和大学学位的儿童比例 此函数应以以下形式返回字典(使用正确的数字,不要对数字进行四舍五入): 我复制并尝试使用的代码如下Python 数据分析不需要任何东西,python,pandas,data-analysis,Python,Pandas,Data Analysis,编写一个名为proportion_of_education的函数,返回数据集中母亲教育程度低于高中(12)和大学学位的儿童比例 此函数应以以下形式返回字典(使用正确的数字,不要对数字进行四舍五入): 我复制并尝试使用的代码如下 def proportion_of_education(): # your code goes here # YOUR CODE HERE # raise NotImplementedError() import pandas as pd
def proportion_of_education():
# your code goes here
# YOUR CODE HERE
# raise NotImplementedError()
import pandas as pd
import numpy as np
df = pd.read_csv("assests/NISPUF17.csv", index_col=0)
EDUS=df['EDUC1']
edus=np.sort(EDUS.values)
poe={"less than high school":0,
"high school":0,
"more than high school but not college":0,
"college":0}
n=len(edus)
poe["less than high school"]=np.sum(edus==1)/n
poe["high school"]=np.sum(edus==2)/n
poe["more than high school but not college"]=np.sum(edus==3)/n
poe["college"]=np.sum(edus==4)/n
return poe
应该打印出来
{"less than high school":0.2,
"high school":0.4,
"more than high school but not college":0.2,
"college":0.2}
然而,笔记本什么也不扔。这不是一个错误,尽管我使用不同的路径多次运行此操作,但绝对不会打印任何内容。有什么问题。你应该使用len(edus==1)
而不是np.sum
,与其他类似的df=pd.read\u csv(“assests/NISPUF17.csv”,index\u col=0)
你必须用下面的行替换这一行,因为你写的资产拼写错误
df=pd.read\u csv(“assets/NISPUF17.csv”,index\u col=0)
正确的代码是:
import pandas as pd
import numpy as np
def proportion_of_education():
df = pd.read_csv("assets/NISPUF17.csv", index_col = 0)
EDUS = df['EDUC1']
edus = np.sort(EDUS.values)
poe = {"less than high school": 0,
"high school": 0,
"more than high school but not college": 0,
"college": 0}
n = len(edus)
poe["less than high school"] = np.sum(edus == 1)/n
poe["high school"] = np.sum(edus == 2)/n
poe["more than high school but not college"] = np.sum(edus == 3)/n
poe["college"] = np.sum(edus == 4)/n
return poe
raise NotImplementedError()
作为pd进口熊猫
将numpy作为np导入
定义教育的比例()
教育的比例()在问题下方的第一个单元格中键入此项以读取数据
import pandas as pd
df=pd.read_csv('assets/NISPUF17.csv',index_col=0)
df
在下一个单元格中
def proportion_of_education():
# your code goes here
cat=pd.value_counts(df['EDUC1'])
total=sum(cat)
a_dict=dict(cat)
s=pd.Series(a_dict)
f=lambda x: x/total
s=s.apply(f)
s_dict=dict(s)
s_dict['less than high school'] = s_dict.pop(1)
s_dict['high school'] = s_dict.pop(2)
s_dict['more than high school but not college'] = s_dict.pop(3)
s_dict['college'] = s_dict.pop(4)
return s_dict
raise NotImplementedError()
proportion_of_education()
要进行检查,请在下一个单元格中键入此项
def proportion_of_education():
# your code goes here
cat=pd.value_counts(df['EDUC1'])
total=sum(cat)
a_dict=dict(cat)
s=pd.Series(a_dict)
f=lambda x: x/total
s=s.apply(f)
s_dict=dict(s)
s_dict['less than high school'] = s_dict.pop(1)
s_dict['high school'] = s_dict.pop(2)
s_dict['more than high school but not college'] = s_dict.pop(3)
s_dict['college'] = s_dict.pop(4)
return s_dict
raise NotImplementedError()
proportion_of_education()
这个问题的最后一个单元在下面
assert type(proportion_of_education())==type({}), "You must return a dictionary."
assert len(proportion_of_education()) == 4, "You have not returned a dictionary with four items in it."
assert "less than high school" in proportion_of_education().keys(), "You have not returned a dictionary with the correct keys."
assert "high school" in proportion_of_education().keys(), "You have not returned a dictionary with the correct keys."
assert "more than high school but not college" in proportion_of_education().keys(), "You have not returned a dictionary with the correct keys."
assert "college" in proportion_of_education().keys(), "You have not returned a dictionary with the correct keys."
在“EDUC1”列中,将有1 2 3 4
=>1-不到高中
=>2-高中
=>3-超过高中,但不超过大学
=>4-大学
lambda函数用于计算所有类别的比例
.pop()方法用于将1重命名为低于高中,类似地,对于其他类别您可以更好地创建词典。可能类似于
poe=df['EDU'].value_计数(normalize=True).to_dict();返回poe代码>这里看起来也只有断言,但没有“打印”语句(如果你想“打印”一些东西,你需要有这些语句)。在正常的脚本/程序中,你必须使用print()
来显示任何东西-print(教育的比例())
或者分两步结果=教育的比例()
打印(结果)
。在记事本中,您也可以使用打印(…)
,或者至少您必须在没有任何其他元素的情况下执行函数-教育的比例()
,笔记本应该自动显示单元格中最后一个函数的结果。但您使用的是教育的比例()
内置断言
,从函数中获取结果,这样笔记本就不会显示任何内容。
assert type(proportion_of_education())==type({}), "You must return a dictionary."
assert len(proportion_of_education()) == 4, "You have not returned a dictionary with four items in it."
assert "less than high school" in proportion_of_education().keys(), "You have not returned a dictionary with the correct keys."
assert "high school" in proportion_of_education().keys(), "You have not returned a dictionary with the correct keys."
assert "more than high school but not college" in proportion_of_education().keys(), "You have not returned a dictionary with the correct keys."
assert "college" in proportion_of_education().keys(), "You have not returned a dictionary with the correct keys."