Skip to content

    Chapter 15, Basic

    Review: the 20 questions

    The original course ends with 20 multiple choice questions. Here each one has the answer and the **reason**, and a code cell that **proves** the 20 statements: if any were wrong, an `assert` would fail.

    The questions and the answers

    #QuestionAnswerWhere
    1s = pd.Series([10, 20, 30]); s[1] prints...20: label 1 (default index)Ch. 4
    2df[["salario"]] returns...A DataFrame with one columnCh. 7
    3With default index, how many rows df.loc[0:3] does it return?4 (the loc includes the end)Ch. 7
    4What shows the missing values by column?df.isnull().sum()Ch. 9
    5What fills in instead of removing?fillna()Ch. 9
    6Best tool to replace values with a dictionary?map()Ch. 11
    7What is wrong with df[df["a"] > 1 and df["b"] > 3]?Missing to use & with parentheses in each conditionCh. 8
    8astype(int) breaks with missing values. The fix?pd.to_numeric(..., errors="coerce") (or a nullable type)Ch. 10
    9Why does a column created with apply get filled with None?The function has no returnCh. 11
    10What method matches any value from a list?isinCh. 8
    11What method gives the average salary by department?groupbyCh. 13
    12How many rows df.iloc[0:5] does it return?5 (the iloc excludes the end)Ch. 7
    13With index id_funcionario, what df.iloc[0] does it return?The first physical row, whatever the labelCh. 7
    14What does the ~ in df[~(df["c"] == "x")]?Negates the conditionCh. 8
    15Why is the dropna() without arguments dangerous?A bad column deletes good rows from othersCh. 9
    16What does the duplicated() compare by default?All columnsCh. 10
    17Why is the to_numeric(coerce) is safer than the astype(float)?Bad values become NaN, and the execution continuesCh. 10
    18Which lambda is valid?lambda x: "Alto" if x > 70000 else "Normal"Ch. 11
    19nlargest(5, c) against sort_values(c).head(5)?The same result, without sorting the entire columnCh. 12
    20Why resumo["departamento"] gives KeyError after a agg?The department turned the indexCh. 13

    The test

    basico/cap15_revisao.pylinhas 10 a 78
    import numpy as np
    import pandas as pd
    
    df = pd.read_csv("dados/funcionarios_15.csv")
    
    
    def levanta(excecao, funcao):
        try:
            funcao()
        except excecao:
            return True
        return False
    
    
    # 1 a 3
    assert pd.Series([10, 20, 30])[1] == 20
    assert isinstance(df[["salario"]], pd.DataFrame)
    assert len(df.loc[0:3]) == 4
    
    # 4 e 5
    assert df.isnull().sum().equals(df.isna().sum()) and int(df.isnull().sum()["cidade"]) == 2
    assert df["idade"].fillna(0).isna().sum() == 0 and len(df.dropna()) < len(df)
    
    # 6
    assert pd.Series(["a", "b"]).map({"a": "x", "b": "y"}).tolist() == ["x", "y"]
    
    # 7: o `and` do Python não funciona em uma Series
    assert levanta(ValueError, lambda: df[(df["experiencia"] > 3) and (df["nota_desempenho"] > 7)])
    
    # 8 e 17: astype(int) quebra, to_numeric(coerce) converte o que dá
    assert levanta(ValueError, lambda: df["idade"].astype(int))
    texto = pd.Series(["10", "abc", "30"])
    assert pd.to_numeric(texto, errors="coerce").tolist()[0] == 10 and pd.to_numeric(texto, errors="coerce").isna().sum() == 1
    
    
    # 9: sem return, tudo vira None
    def sem_return(x):
        if x > 3:
            "alto"
    
    
    assert df["experiencia"].apply(sem_return).isna().all()
    
    # 10 e 11
    assert df["departamento"].isin(["Engenharia", "RH"]).sum() == 5
    assert df.groupby("departamento")["experiencia"].mean().notna().all()
    
    # 12 e 13
    assert len(df.iloc[0:5]) == 5
    por_id = df.set_index("id_funcionario")
    assert por_id.iloc[0]["nome"] == "Ana Souza" and levanta(KeyError, lambda: por_id.loc[0])
    
    # 14
    assert (~(df["cidade"] == "Curitiba")).sum() == len(df) - 2
    
    # 15 e 16
    assert len(df.dropna()) == 9 and int(df.duplicated().sum()) == 0
    
    # 18
    assert (lambda x: "Alto" if x > 70000 else "Normal")(80000) == "Alto"
    
    # 19
    assert df.nlargest(5, "projetos_concluidos")["projetos_concluidos"].tolist() == \
        df.sort_values("projetos_concluidos", ascending=False).head(5)["projetos_concluidos"].tolist()
    
    # 20
    resumo = df.groupby("departamento").agg(media=("experiencia", "mean"))
    assert levanta(KeyError, lambda: resumo["departamento"]) and "departamento" in resumo.reset_index().columns
    print("as 20 respostas conferem")
    
    Output
    as 20 respostas conferem
    

    If you got any question wrong, the best thing is to go back to the chapter of the last column of the table, run the example and change the code to see what happens. Memorizing the answer doesn't help: what remains is having seen Pandas fail.