Question bank

Pick an answer to see if you're right — with an explanation.

PandasMedium
Which is the fastest way to create a column that is `amount * 0.1` for members and 0 otherwise?
adf.apply(lambda r: ..., axis=1)
bA for loop with iterrows()
cdf.itertuples() with a list
dnp.where(df['member'], df['amount'] * 0.1, 0)
PandasHard
What is printed?
a = pd.Series([1, 2], index=['x', 'y'])
b = pd.Series([10, 20], index=['y', 'z'])
print((a + b).tolist())
a[nan, 12.0, nan]
b[11, 22]
c[1, 12, 20]
dA ValueError about mismatched lengths
PandasHard
What does this produce?
g = df.groupby(["branch", "category"])["amount"].sum()
t = g.unstack("category", fill_value=0)
aA long Series with a 3-level index
bA DataFrame with branches as rows and categories as columns
cA DataFrame with categories as rows and branches as columns
dAn error because unstack needs a DataFrame
PandasHard
After `pd.concat([df1, df2])`, `df.loc[0]` returns two rows. Why?
aconcat always duplicates the first row
bBoth frames kept their original index labels; pass ignore_index=True to renumber
c`loc` always returns a DataFrame
dThe two frames have different columns
PandasHard
Which statement about `df[df['city'] == 'Pune']['amount'] = 0` is correct?
aIt reliably sets Pune amounts to 0
bIt raises a SyntaxError
cIt deletes Pune rows
dIt may modify a temporary copy, leaving df unchanged; use df.loc[mask, 'amount'] = 0