Python for Data course menu
Beginner pandas and NumPy Interview Questions and Answers
Common data analyst interview questions for freshers on NumPy and pandas, with clear answers and runnable examples: arrays vs lists, Series vs DataFrame, loc vs iloc, missing values, filtering, groupby, merge and value_counts.
On this page
- Concepts
- 1. Why use a NumPy array instead of a Python list?
- 2. What is the difference between a Series and a DataFrame?
- 3. loc vs iloc?
- 4. How do you find and handle missing values?
- 5. What does value_counts do?
- Coding questions
- 6. Filter rows with two conditions.
- 7. Total and average sales per region.
- 8. Join customers to their orders, keeping customers with no orders.
- 9. Remove duplicate rows, keeping the last.
- 10. Add a column based on a condition.
- Key takeaway
These questions come up in nearly every data analyst screening. Answer out loud first, then open the solution. Short, precise answers with a tiny example beat memorised paragraphs.
Concepts#
1. Why use a NumPy array instead of a Python list?#
Show solution
An array stores values of one type together in memory, so NumPy runs maths on the whole array in compiled code (vectorisation): far faster than looping over a list, and much shorter to write. Arithmetic also means maths, not list operations:
import numpy as np
print([1, 2, 3] * 2)
print(np.array([1, 2, 3]) * 2)[1, 2, 3, 1, 2, 3]
[2 4 6]2. What is the difference between a Series and a DataFrame?#
Show solution
A Series is one labelled column of values; a DataFrame is a table of Series sharing one row index. df["col"] returns a Series, df[["col"]] a one-column DataFrame.
3. loc vs iloc?#
Show solution
loc selects by label (index values, column names) and includes the end of a slice; iloc selects by position and excludes the end:
import pandas as pd
s = pd.Series([10, 20, 30, 40], index=["a", "b", "c", "d"])
print(s.loc["a":"c"].tolist(), s.iloc[0:2].tolist())[10, 20, 30] [10, 20]4. How do you find and handle missing values?#
Show solution
Find them with df.isna().sum(). Then, depending on how many there are and why: drop rows (dropna, with subset or thresh), fill them (fillna with a constant, the median, a group mean, or forward fill for time series), or keep them and add a flag column. Calculations like mean() skip NaN by default.
5. What does value_counts do?#
Show solution
It counts how often each value appears in a Series, sorted from most to least common; normalize=True gives proportions.
import pandas as pd
print(pd.Series(["upi", "card", "upi", "cash", "upi"]).value_counts().to_dict()){'upi': 3, 'card': 1, 'cash': 1}Coding questions#
6. Filter rows with two conditions.#
Show solution
Use & and | with parentheses around each condition (not and/or):
import pandas as pd
df = pd.DataFrame({"city": ["Mumbai", "Delhi", "Mumbai"], "amount": [250, 700, 900]})
print(df[(df["city"] == "Mumbai") & (df["amount"] > 300)]) city amount
2 Mumbai 9007. Total and average sales per region.#
Show solution
import pandas as pd
df = pd.DataFrame({"region": ["N", "S", "N", "S", "E"], "sales": [100, 80, 140, 60, 90]})
print(df.groupby("region")["sales"].agg(["sum", "mean"])) sum mean
region
E 90 90.0
N 240 120.0
S 140 70.08. Join customers to their orders, keeping customers with no orders.#
Show solution
A left merge keeps every row of the left table:
import pandas as pd
customers = pd.DataFrame({"id": [1, 2, 3], "name": ["Asha", "Ravi", "Meera"]})
orders = pd.DataFrame({"id": [1, 1, 2], "amount": [250, 120, 560]})
print(pd.merge(customers, orders, on="id", how="left")) id name amount
0 1 Asha 250.0
1 1 Asha 120.0
2 2 Ravi 560.0
3 3 Meera NaN9. Remove duplicate rows, keeping the last.#
Show solution
import pandas as pd
df = pd.DataFrame({"email": ["a@x.in", "b@x.in", "a@x.in"], "plan": ["basic", "pro", "pro"]})
print(df.drop_duplicates(subset="email", keep="last")) email plan
1 b@x.in pro
2 a@x.in pro10. Add a column based on a condition.#
Show solution
import numpy as np
import pandas as pd
df = pd.DataFrame({"score": [45, 72, 88]})
df["result"] = np.where(df["score"] >= 50, "pass", "fail")
print(df) score result
0 45 fail
1 72 pass
2 88 passKey takeaway#
Next: Intermediate Questions.
Finished this lesson?