181
What is NumPy broadcasting? Explain with an example.
Short Answer
Broadcasting allows NumPy to perform operations on arrays of different shapes by automatically expanding the smaller array. Rules: dimensions compared from right, must be equal or one of them is 1.
import numpy as np
a = np.array([[1, 2, 3], # Shape: (2, 3)
[4, 5, 6]])
b = np.array([10, 20, 30]) # Shape: (3,)
result = a + b # b broadcasts to (2, 3)
# [[11, 22, 33],
# [14, 25, 36]]
💡 Memory Trick: "Broadcasting = stretching the smaller array to fit (like wallpaper pattern repeating)"
182
What is the difference between loc and iloc in Pandas?
Short Answer
loc uses label-based indexing (row/column names). iloc uses integer position-based indexing (0, 1, 2...).
import pandas as pd
df = pd.DataFrame({'A': [1,2,3], 'B': [4,5,6]},
index=['x','y','z'])
df.loc['x', 'A'] # Label-based → 1
df.iloc[0, 0] # Position-based → 1
df.loc['x':'y'] # Inclusive on both ends
df.iloc[0:2] # Exclusive on end (like Python) 💡 Memory Trick: "loc = Label Of Column/row | iloc = Integer Location"
Questions 184-200 continue with the same detailed format.
Topics covered: GroupBy operations, Merge/Join types, Vectorization vs Apply, Memory optimization (dtypes), Pivot Tables, Multi-index, Rolling/Window functions, NumPy vectorization, and performance comparison with pure Python.
View Full Documentation →