Intermediate 20 Questions • ~5 hrs study

NumPy & Pandas (Q181-200)

Essential NumPy and Pandas questions for data science and ML interviews — array operations, broadcasting, DataFrames, and performance tips.

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"

183

How do you handle missing data in Pandas?

Short Answer

Use isnull()/isna() to detect, fillna() to fill, dropna() to remove. Choose strategy based on data context: mean/median for numerical, mode for categorical, forward/backward fill for time series.

df.isnull().sum()               # Count missing per column
df.fillna(df.mean())            # Fill with column mean
df.fillna(method='ffill')       # Forward fill
df.dropna(subset=['important']) # Drop rows where specific col is NaN
df.interpolate()                # Linear interpolation

💡 Memory Trick: "Missing data strategy: Detect → Decide (fill/drop/interpolate) → Validate"

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 →