Skip to main contentSkip to solution

What will be the output of the following python code?

import numpy as np

import pandas as pd

array1 = np.array([10,20,30])

array2 = np.array([100,200,300])

array3 = np.array([-10,-20,-30, -40])

dFrame = pd.DataFrame([array1, array3, array2], columns=[ 'A', 'B', 'C', 'D'])

print(dFrame)

Solution

✅ Correct Option: 1

The list passed to the DataFrame is [array1, array3, array2], and each array becomes one row in that order. Row 0 is [10, 20, 30], row 1 is [-10, -20, -30, -40] and row 2 is [100, 200, 300].

The columns are A, B, C, D. The arrays in rows 0 and 2 have only 3 values, so column D is filled with NaN for those rows. Because NaN is a float, column D becomes float, which is why -40 prints as -40.0. Columns A, B and C stay integer.


Option 2: pandas shows missing values as NaN, not Null.

Options 3 and 4: rows follow the order in the list (array1, array3, array2), so array3 must be row 1.

Keyboard Shortcuts

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