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