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.
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
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.
Related questions:
2025: 30 May Shift 2
2026: 26 May Shift 1
2025: 20 May Shift 1