Question:

Complete the given Python code to generate the following output:
Expected output:
   COLOUR  NAME  QTY
0     Red  Apple   10
1    Blue  Berry   15
2   Green  Guava   20

Given incomplete code:
import _______ as pd
data = [
    {'COLOUR': 'Red', 'NAME': 'Apple', 'QTY': 10},
    {'COLOUR': 'Blue', 'NAME': 'Berry', 'QTY': 15},
    {_______, 'NAME': 'Guava', 'QTY': 20}
]
df = pd.DataFrame(_______)
print(_______)

Show Hint

Always check that each dictionary in your list has the same keys for smooth DataFrame creation.
Updated On: Jul 14, 2025
Hide Solution
collegedunia
Verified By Collegedunia

Solution and Explanation

Completed code:
import pandas as pd

data = [
    {'COLOUR': 'Red', 'NAME': 'Apple', 'QTY': 10},
    {'COLOUR': 'Blue', 'NAME': 'Berry', 'QTY': 15},
    {'COLOUR': 'Green', 'NAME': 'Guava', 'QTY': 20}
]

df = pd.DataFrame(data)
print(df)

Corrections made:
1. Filled the first blank with `pandas` to import the library.
2. Added `'COLOUR': 'Green'` for the third dictionary to ensure all rows have the same keys.
3. Used `data` inside `pd.DataFrame()` to create the DataFrame.
4. Printed the DataFrame with `print(df)` to display the output as given.
This ensures the DataFrame matches the required structure and prints the correct table.
Was this answer helpful?
0
0

Top Questions on Python Libraries

View More Questions