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
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
A DataFrame can be built from a dictionary of lists, where keys become column names, as in .
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 , 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.
Related questions:
2026: 29 May Shift 2
2026: 11 May Shift 2
2025: 22 May Shift 1
2026: 26 May Shift 1