Skip to main contentSkip to solution

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

✅ Correct Option: 4

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.

Keyboard Shortcuts

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