Sample questions
Question 1Easy
What does this print?
import pandas as pd
df = pd.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]})
print(df.shape)
- (3, 2)
- 6
- (3,)
- (2, 3)
Answer: A. (3, 2)
Explanation
3 rows and 2 columns.
Step-by-step solution
- Each list has 3 values, so there are 3 rows.
- Columns a and b give 2 columns.
- shape = (3, 2).
Why the other options are wrong
- B. 6 is df.size, not shape.
- C. (3,) is a Series shape.
- D. (2, 3) swaps rows and columns.
Tip: df.shape = (rows, columns).
Question 2Easy
What does this print?
import pandas as pd
s = pd.Series([10, 20, 30])
print(s.sum())
- [10, 20, 30]
- 20.0
- 60
- 30
Answer: C. 60
Explanation
sum adds all values.
Step-by-step solution
s.sum() adds all values.- 10 + 20 + 30 = 60.
- Integers stay integers: 60.
Why the other options are wrong
- A. sum returns one number, not the list.
- B. 20.0 is the mean.
- D. 30 is the maximum.
Tip: sum keeps the dtype; mean returns a float.
Question 3Medium
What does this print?
import pandas as pd
d = pd.to_datetime(pd.Series(['2024-01-15', '2024-03-02']))
print(d.dt.month.tolist())
- ['Jan', 'Mar']
- [15, 2]
- [2024, 2024]
- [1, 3]
Answer: D. [1, 3]
Explanation
The .dt accessor exposes date parts such as month.
Step-by-step solution
- to_datetime parses the strings into dates.
- .dt.month extracts the month number.
- January = 1, March = 3.
Why the other options are wrong
- A. month returns numbers; month_name() returns names.
- B. Those are the day values.
- C. Those are the years.
Tip: Parse with to_datetime, then use .dt.
Question 4Medium
What does this print?
import pandas as pd
df = pd.DataFrame({'p': [100, 200], 'q': [3, 2]})
df['total'] = df['p'] * df['q']
print(df['total'].sum())
- 500
- 600
- 700
- 300
Answer: C. 700
Explanation
300 + 400 = 700.
Step-by-step solution
- total = p * q row by row:
100 x 3 = 300,
200 x 2 = 400.
- Sum = 300 + 400
= 700.
Why the other options are wrong
- A. 500 is not 300 + 400; check each product.
- B. 600 is not the sum of the products.
- D. 300 is only the first row.
Tip: New column = expression of existing columns.
Question 5Medium
What does this print?
import pandas as pd
df = pd.DataFrame({'d': ['A', 'A', 'B'], 'm': ['J', 'F', 'J'], 'v': [1, 2, 3]})
p = df.pivot_table(index='d', columns='m', values='v', aggfunc='sum')
print(p.shape)
- (2, 3)
- (3, 3)
- (2, 2)
- (3, 2)
Answer: C. (2, 2)
Explanation
Rows: A and B; columns: F and J.
Step-by-step solution
- Index values (d): A, B gives 2 rows.
- Column values (m): F, J gives 2 columns.
- shape = (2, 2).
Why the other options are wrong
- A. There are only 2 distinct m values.
- B. There are only 2 distinct d values.
- D. Rows come from d (2), not from the original 3 rows.
Tip: Pivot shape = (unique index values, unique column values).
Question 6Medium
What does this print?
import pandas as pd
df = pd.DataFrame({'a': [3, 1, 2], 'b': ['x', 'y', 'z']})
print(df.sort_values('a', ascending=False)['b'].tolist())
- ['x', 'y', 'z']
- ['z', 'y', 'x']
- ['x', 'z', 'y']
- ['y', 'z', 'x']
Answer: C. ['x', 'z', 'y']
Explanation
Sorting a descending gives 3, 2, 1, whose b values are x, z, y.
Step-by-step solution
- Sort by a descending: 3, 2, 1.
- Matching b values: x (a = 3), z (a = 2), y (a = 1).
- Result: ['x', 'z', 'y'].
Why the other options are wrong
- A. That is the original order.
- B. That reverses b, not sorting by a.
- D. That is ascending order of a.
Tip: sort_values moves entire rows.
Question 7Medium
What does this print?
import pandas as pd
df = pd.DataFrame({'s': [50, 75, 90]})
df['grade'] = pd.cut(df['s'], bins=[0, 60, 80, 100], labels=['C', 'B', 'A'])
print(df['grade'].tolist())
- ['C', 'B', 'A']
- ['B', 'B', 'A']
- ['C', 'C', 'B']
- ['A', 'B', 'C']
Answer: A. ['C', 'B', 'A']
Explanation
Bins are (0, 60], (60, 80], (80, 100] by default.
Step-by-step solution
- Bins: (0, 60] is C, (60, 80] is B, (80, 100] is A.
- 50 is in (0, 60] so C; 75 in (60, 80] so B; 90 in (80, 100] so A.
- Result: ['C', 'B', 'A'].
Why the other options are wrong
- B. 50 falls in the first bin, labelled C.
- C. 75 and 90 fall in higher bins.
- D. Labels follow bin order, low to high.
Tip: cut bins are (left, right] by default.
Question 8Medium
What does this print?
import pandas as pd
df = pd.DataFrame({'t': ['a', 'b', 'a']})
print(df['t'].nunique())
- ['a', 'b']
- 2
- 3
- 1
Answer: B. 2
Explanation
nunique counts distinct values.
Step-by-step solution
- Values: a, b, a.
- Distinct values: a and b.
nunique() = 2.
Why the other options are wrong
- A. That is unique(), which lists values.
- C. 3 is the row count.
- D. There are two distinct values.
Tip: unique lists; nunique counts.
Question 9Hard
What does this print?
import pandas as pd
s = pd.Series([10, 20, 30, 40], index=[1, 2, 3, 4])
print(s.loc[1:2].tolist(), s.iloc[1:2].tolist())
- [10, 20] [20, 30]
- [10, 20] [20]
- [20] [20]
- [10] [20]
Answer: B. [10, 20] [20]
Explanation
loc slicing by label includes both ends; iloc slicing by position excludes the end.
Step-by-step solution
- loc[1:2] slices by label and includes the end: labels 1 and 2 give [10, 20].
- iloc[1:2] slices by position and excludes the end: position 1 only, [20].
- Output: [10, 20] [20].
Why the other options are wrong
- A. iloc excludes the stop position, so it returns only one value.
- C. loc[1:2] starts at label 1, which is 10.
- D. loc includes the end label 2.
Tip: loc slices include the end; iloc slices do not.
Question 10Hard
What does this print?
import pandas as pd
df = pd.DataFrame({'a': [1, 2, 3], 'b': [10, 20, 30]})
print(df.query('a >= 2 and b < 30')['b'].tolist())
- [20]
- []
- [10, 20]
- [20, 30]
Answer: A. [20]
Explanation
Only the row a = 2, b = 20 satisfies both conditions.
Step-by-step solution
a >= 2 keeps rows 2 and 3 (b = 20, 30).b < 30 keeps b = 20 only.- Result:
[20].
Why the other options are wrong
- B. Row a = 2, b = 20 satisfies both conditions.
- C. a = 1 fails a >= 2.
- D. b = 30 fails b < 30.
Tip: query('cond1 and cond2') = both must hold.
Practise all 36 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →