编写一个函数proportion_of_education,返回数据集中母亲的教育水平等于高中以下(〈12)、高中(12)、高中以上但不是大学毕业生(〉12)和大学学位的儿童比例。
这个函数应该返回一个字典的形式(使用正确的数字,不要舍入数字):
{"less than high school":0.2,
"high school":0.4,
"more than high school but not college":0.2,
"college":0.2}
我复制并尝试使用的代码如下
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
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."
应该是打印出来的
{"less than high school":0.2,
"high school":0.4,
"more than high school but not college":0.2,
"college":0.2}
然而,Notebook什么也没有抛出。不是一个错误,尽管我使用不同的路径运行了多次,但绝对什么也没有打印出来。可能是什么问题呢?
9条答案
按热度按时间k7fdbhmy1#
您应该使用
len(edus==1)
而不是np.sum
,并且与其他类似fivyi3re2#
df = pd.read_csv("assests/NISPUF17.csv", index_col=0)
你必须用下面的行替换这一行,因为你写的资产拼写错误。df = pd.read_csv("assets/NISPUF17.csv", index_col=0)
j2datikz3#
正确的代码是:
fnx2tebb4#
import pandas as pd import numpy as np
def proportion_of_education():
proportion_of_education()
vpfxa7rd5#
在问题下面的第一个单元格中键入此内容以读取数据
在下一个单元格
要检查,请在下一个单元格中键入以下内容
这个问题的最后一个单元格在下面
在“EDUC 1”列中,将有1 2 3 4
=〉1 -小于高中
=〉高中
=〉3 -超过高中但不是大学
=〉4 -学院
lambda函数用于计算所有类别的比例
.pop()方法用于将1重命名为小于高中,其他类别也是如此
ozxc1zmp6#
sq1bmfud7#
简单的解决方案
xdyibdwo8#
简单的解决方法是:
df=pd.read_csv(“assets/NISPUF17.csv”,index_col=0)
uemypmqf9#