Question bank
Pick an answer to see if you're right — with an explanation.
All topicsPythonSQL / MySQLData ScienceNumPyPandasMatplotlibMachine LearningDeep LearningArtificial IntelligenceLarge Language ModelsDjangoAPIsGit & GitHubPower BIExcelStatistics
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)
Explanation: np.where works on whole columns in compiled code instead of calling Python per row.
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
Explanation: Arithmetic aligns on the index, so only the shared label 'y' has both values and labels present in just one Series become NaN.
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
Explanation: unstack moves the 'category' index level into columns, leaving branch as the row index.
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
Explanation: concat stacks the frames without touching the index, so labels repeat; ignore_index=True (or reset_index(drop=True)) gives a fresh 0..n-1 index.
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
Explanation: This is chained assignment; a single .loc call assigns to the original DataFrame.