Skip to main contentSkip to solution

Arrange the following statements for creating a DataFrame:

(A) Create a DataFrame

(B) Display the DataFrame.

(C) Import the pandas library.

(D) Assign column headings.

Solution

✅ Correct Option: 4

The pandas library must be imported first, because the DataFrame is built using it. Column headings are then assigned to the created DataFrame, and it is displayed last.

(C) Import the pandas library, e.g. import pandas as pd

(A) Create a DataFrame, e.g. df = pd.DataFrame(data)

(D) Assign column headings, e.g. df.columns = [...] or the columns= argument

(B) Display the DataFrame, e.g. print(df)


Options 1, 2 and 3: they create or use a DataFrame before importing pandas, or display it before it exists, which gives an error.

Keyboard Shortcuts

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