Skip to main contentSkip to solution

Which of the following are valid ways to create a pandas DataFrame?

(A) From the dictionary of the lists

(B) From a list of dictionaries

(C) From a NumPy array

(D) From a scalar value

Solution

✅ Correct Option: 2

A DataFrame can be built from a dictionary of lists, where keys become column names, as in pd.DataFrame({’a’:[1,2],’b’:[3,4]})\text{pd.DataFrame}(\{\text{'a'}: [1,2], \text{'b'}: [3,4]\}).

It can also be built from a list of dictionaries, where each dictionary becomes one row, and from a NumPy array, where a 2D array gives rows and columns.


Option (D): passing only a scalar, as in pd.DataFrame(5)\text{pd.DataFrame}(5), raises a ValueError because pandas cannot tell the shape. A scalar works only when index and columns are also given, so it is not a valid way on its own.

Keyboard Shortcuts

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