Consider the following code creating a DataFrame from a dictionary of lists. What is the shape (rows, columns) of the resulting DataFrame?
import pandas as pd data = {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]} df = pd.DataFrame(data) print(df.shape)
Each key in the dictionary becomes a column, and each list item becomes a row.
The DataFrame has 3 columns (A, B, C) and 3 rows because each list has 3 elements.
Given this code, what is the content of the DataFrame?
import pandas as pd data = {'Name': ['Alice', 'Bob'], 'Age': [25, 30]} df = pd.DataFrame(data) print(df)
Default index starts at 0 and increments by 1.
The DataFrame shows two rows with index 0 and 1, columns 'Name' and 'Age'.
Look at this code snippet. Why does it raise an error?
import pandas as pd data = {'A': [1, 2], 'B': [3, 4, 5]} df = pd.DataFrame(data)
All columns must have the same number of elements.
The lists for columns 'A' and 'B' have different lengths (2 and 3), causing a ValueError.
Which statement best explains why the method of creating a DataFrame matters for performance?
Think about how pandas aligns data internally when creating from different structures.
Creating from a list of dicts requires pandas to align keys for each row, which is slower than creating from dict of lists where columns are aligned directly.
Choose the code snippet that creates a DataFrame with a MultiIndex on rows.
MultiIndex is created explicitly using pandas MultiIndex class.
Option A uses pd.MultiIndex.from_tuples to create a MultiIndex and passes it as index to the DataFrame.