Which of the following ways can be used to create a DataFrame in Python?
(A) NumPy ndarrays
(B) Lists
(C) Lists of Dictionary
(D) Strings
Which of the following ways can be used to create a DataFrame in Python?
(A) NumPy ndarrays
(B) Lists
(C) Lists of Dictionary
(D) Strings
Solution
pandas DataFrame() accepts NumPy ndarrays, lists (including nested lists) and lists of dictionaries, where each dictionary becomes one row and the keys become column names.
For example, pd.DataFrame([{'a': 1, 'b': 2}, {'a': 3, 'b': 4}]) creates a 2-row DataFrame with columns a and b.
Option 3: a plain string is not a valid data source. pd.DataFrame("abc") raises an error because the constructor needs a collection of data, not a single scalar.
Option 1: it leaves out lists of dictionaries, which are a standard way to build a DataFrame.
Related questions:
2026: 18 May Shift 2
2026: 23 May Shift 1
2025: 22 May Shift 1
2026: 29 May Shift 2