使用Pandas进行数据分析不会抛出任何结果

8aqjt8rx  于 2023-04-19  发布在  其他
关注(0)|答案(9)|浏览(96)

编写一个函数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什么也没有抛出。不是一个错误,尽管我使用不同的路径运行了多次,但绝对什么也没有打印出来。可能是什么问题呢?

k7fdbhmy

k7fdbhmy1#

您应该使用len(edus==1)而不是np.sum,并且与其他类似

fivyi3re

fivyi3re2#

df = pd.read_csv("assests/NISPUF17.csv", index_col=0)你必须用下面的行替换这一行,因为你写的资产拼写错误。
df = pd.read_csv("assets/NISPUF17.csv", index_col=0)

j2datikz

j2datikz3#

正确的代码是:

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()
fnx2tebb

fnx2tebb4#

import pandas as pd import numpy as np
def proportion_of_education():

# your code goes here
# YOUR CODE HERE

df = pd.read_csv(r'assets/NISPUF17.csv',index_col=0)
EDUC1 = df['EDUC1']
educ1 = np.sort(EDUC1.values)
poe = {"less than high school": 0,
    "high school": 0,
    "more than high school but not college": 0,
    "college": 0}
total = len(educ1)
poe['less than high school'] = len(df[df['EDUC1'] == 1])/total
poe['high school'] = len(df[df['EDUC1'] == 2])/total
poe['more than high school but not college'] = len(df[df['EDUC1'] == 3])/total
poe['college'] = len(df[df['EDUC1'] == 4])/total
return poe
raise NotImplementedError()

proportion_of_education()

vpfxa7rd

vpfxa7rd5#

在问题下面的第一个单元格中键入此内容以读取数据

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()

这个问题的最后一个单元格在下面

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."

在“EDUC 1”列中,将有1 2 3 4
=〉1 -小于高中
=〉高中
=〉3 -超过高中但不是大学
=〉4 -学院
lambda函数用于计算所有类别的比例
.pop()方法用于将1重命名为小于高中,其他类别也是如此

ozxc1zmp

ozxc1zmp6#

import pandas as pd
    import numpy as np
    def proportion_of_education():
    # your code goes here
    # YOUR CODE HERE
        df=pd.read_csv('assets/NISPUF17.csv',index_col=0)
        poe = {"less than high school": len(df[df['EDUC1'] == 1])/len(df),
"high school": len(df[df['EDUC1'] == 2])/len(df),
"more than high school but not college": len(df[df['EDUC1'] == 3])/len(df),
"college": len(df[df['EDUC1'] == 4])/len(df)}
      return poe

    proportion_of_education()
sq1bmfud

sq1bmfud7#

简单的解决方案

def proportion_of_education():
    import pandas as pd

    df = pd.read_csv("NISPUF17.csv")

    df = df["EDUC1"]
    lessThanHighSchool = (df[df == 1].count()) / df.count()
    highScool = (df[df == 2].count()) / df.count()
    moreThanHighSchoolNotCollage = (df[df == 3].count()) / df.count()
    collage = (df[df == 4].count()) / df.count()

    dictionary = {"less than high school ":lessThanHighSchool ,
        "high school":highScool,
        "more than high school but not college":moreThanHighSchoolNotCollage,
        "college":collage
        }

    return dictionary
xdyibdwo

xdyibdwo8#

简单的解决方法是:
df=pd.read_csv(“assets/NISPUF17.csv”,index_col=0)

df['EDUC1 Label'] =df["EDUC1"].map({
    1:"less than high school",
    2:"high school",
    3:"more than high school but not college",
    4:"college"})
total=df["EDUC1 Label"].value_counts(normalize=True).to_dict()
return total
uemypmqf

uemypmqf9#

def proportion_of_education():
  import pandas as pd
  df = pd.read_csv("assets/NISPUF17.csv", index_col= 0)
  EDUC1 = df['EDUC1']
  less = len(df[df['EDUC1']== 1]['EDUC1'])/len(EDUC1)
  high = len(df[df['EDUC1']== 2]['EDUC1'])/len(EDUC1)
  more = len(df[df['EDUC1']== 3]['EDUC1'])/len(EDUC1)
  college = len(df[df['EDUC1']== 4]['EDUC1'])/len(EDUC1)
  poe = {"less than high school":less,
  "high school":high,
  "more than high school but not college":more,
  "college":college}
  return poe
  raise NotImplementedError()
proportion_of_education()

相关问题