Skip to main contentSkip to solution

What will be the correct sequence of the following statements to create a series from a one-dimensional (1D) NumPy array?

(A) >>> import pandas as pd

(B) >>> s = pd.Series(a)

(C) >>> a = np.array([1,2,3,4])

(D) >>> import numpy as np

Solution

✅ Correct Option: 3

np must be imported before np.array() can be used, and a must exist before it is passed to pd.Series(), which in turn needs pandas imported as pd. So (D) comes before (C), and (C) and (A) both come before (B).

(D) >>> import numpy as np

(C) >>> a = np.array([1,2,3,4])

(A) >>> import pandas as pd

(B) >>> s = pd.Series(a)


Options 1 and 4: (C) uses np before numpy is imported in (D), which gives a NameError.

Option 2: (B) uses pd and a before either is defined.

Keyboard Shortcuts

  • Left arrow: Previous question
  • Right arrow: Next question
  • S key: Jump to solution
  • Q key: Jump to question