Fill in the blanks with an appropriate command from the given options to…

2023

Fill in the blanks with an appropriate command from the given options to produce the output shown on the right side of the following code: import pandas as pd D=[[1,2],[3,4]] A=["A1", "A2"] B=["B1", "B2"] Print(D)

image.pngimage.png
  1. A.

    D=pd.DataFrame(D, columns=A index=B) / D= pd.DataFrame(D, columns = A, index=B)

  2. B.

    D=pd.DataFrame(D,rows=B,columns=A) / D= pd.DataFrame(D,rows=B,columns=A)

  3. C.

    D=pd.DataFrame(D, B, i=A) / D=pd.DataFrame(D, B, i=A)

  4. D.

    D=pd.DataFrame(A,B,D) / D=pd.DataFrame(A, B, D)

Attempted by 898 students.

Show answer & explanation

Correct answer: A

Solution:

Use pandas.DataFrame with the data as the first argument and named parameters columns and index to set column and row labels.

  • Correct command: D = pd.DataFrame(D, columns=A, index=B)

  • Explanation: The first argument is the data (the list of lists). columns= assigns the column labels A1, A2. index= assigns the row labels B1, B2.

  • Why other forms fail: using 'rows' or 'i' are invalid parameter names, and passing labels as positional arguments swaps meaning or causes errors. Always use columns= and index= for labels.

Explore the full course: Bpsc