pandas Questions with Answers

36 practice questions on pandas (Data Analytics) with answers and step-by-step solutions. Try 10 samples below, then practise all 36 free in the AptiNest app.

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)
  1. (3, 2)
  2. 6
  3. (3,)
  4. (2, 3)

Answer: A. (3, 2)

Explanation

3 rows and 2 columns.

Step-by-step solution

  1. Each list has 3 values, so there are 3 rows.
  2. Columns a and b give 2 columns.
  3. 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())
  1. [10, 20, 30]
  2. 20.0
  3. 60
  4. 30

Answer: C. 60

Explanation

sum adds all values.

Step-by-step solution

  1. s.sum() adds all values.
  2. 10 + 20 + 30 = 60.
  3. 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())
  1. ['Jan', 'Mar']
  2. [15, 2]
  3. [2024, 2024]
  4. [1, 3]

Answer: D. [1, 3]

Explanation

The .dt accessor exposes date parts such as month.

Step-by-step solution

  1. to_datetime parses the strings into dates.
  2. .dt.month extracts the month number.
  3. 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())
  1. 500
  2. 600
  3. 700
  4. 300

Answer: C. 700

Explanation

300 + 400 = 700.

Step-by-step solution

  1. total = p * q row by row: 100 x 3 = 300, 200 x 2 = 400.
  2. 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)
  1. (2, 3)
  2. (3, 3)
  3. (2, 2)
  4. (3, 2)

Answer: C. (2, 2)

Explanation

Rows: A and B; columns: F and J.

Step-by-step solution

  1. Index values (d): A, B gives 2 rows.
  2. Column values (m): F, J gives 2 columns.
  3. 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())
  1. ['x', 'y', 'z']
  2. ['z', 'y', 'x']
  3. ['x', 'z', 'y']
  4. ['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

  1. Sort by a descending: 3, 2, 1.
  2. Matching b values: x (a = 3), z (a = 2), y (a = 1).
  3. 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())
  1. ['C', 'B', 'A']
  2. ['B', 'B', 'A']
  3. ['C', 'C', 'B']
  4. ['A', 'B', 'C']

Answer: A. ['C', 'B', 'A']

Explanation

Bins are (0, 60], (60, 80], (80, 100] by default.

Step-by-step solution

  1. Bins: (0, 60] is C, (60, 80] is B, (80, 100] is A.
  2. 50 is in (0, 60] so C; 75 in (60, 80] so B; 90 in (80, 100] so A.
  3. 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())
  1. ['a', 'b']
  2. 2
  3. 3
  4. 1

Answer: B. 2

Explanation

nunique counts distinct values.

Step-by-step solution

  1. Values: a, b, a.
  2. Distinct values: a and b.
  3. 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())
  1. [10, 20] [20, 30]
  2. [10, 20] [20]
  3. [20] [20]
  4. [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

  1. loc[1:2] slices by label and includes the end: labels 1 and 2 give [10, 20].
  2. iloc[1:2] slices by position and excludes the end: position 1 only, [20].
  3. 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())
  1. [20]
  2. []
  3. [10, 20]
  4. [20, 30]

Answer: A. [20]

Explanation

Only the row a = 2, b = 20 satisfies both conditions.

Step-by-step solution

  1. a >= 2 keeps rows 2 and 3 (b = 20, 30).
  2. b < 30 keeps b = 20 only.
  3. 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 →

Related topics

See all Data Analytics topics · All topics