A DataFrame is a table-like data structure in pandas that stores data in rows and columns, similar to a spreadsheet or SQL table.
Click to reveal answer
beginner
How do you create a DataFrame from a dictionary in pandas?
Use pd.DataFrame() and pass the dictionary as an argument. Keys become column names, and values become the column data.
Click to reveal answer
intermediate
What happens if dictionary values have different lengths when creating a DataFrame?
pandas will raise a ValueError because all columns must have the same number of elements.
Click to reveal answer
beginner
Example: Create a DataFrame from {'Name': ['Alice', 'Bob'], 'Age': [25, 30]}. What will be the columns?
The DataFrame will have two columns: Name and Age. Each row corresponds to a person with their name and age.
Click to reveal answer
intermediate
Can dictionary keys be used as row labels (index) when creating a DataFrame?
No, dictionary keys become column names. To set row labels, use the index parameter separately.
Click to reveal answer
What does each key in the dictionary represent when creating a DataFrame?
AA column name
BA row label
CA cell value
DAn index number
✗ Incorrect
Each key in the dictionary becomes a column name in the DataFrame.
Which pandas function is used to create a DataFrame from a dictionary?
Apd.DataFrame()
Bpd.dict()
Cpd.array()
Dpd.Series()
✗ Incorrect
pd.DataFrame() creates a DataFrame from various data structures including dictionaries.
What error occurs if dictionary values have different lengths when creating a DataFrame?
ATypeError
BValueError
CKeyError
DIndexError
✗ Incorrect
pandas raises a ValueError because columns must have the same length.
If you want to set custom row labels when creating a DataFrame from a dictionary, which parameter do you use?
Acolumns
Bkeys
Clabels
Dindex
✗ Incorrect
The index parameter sets custom row labels.
What will be the output columns for {'City': ['NY', 'LA'], 'Population': [8, 4]}?
A8 and 4
BNY and LA
CCity and Population
DNo columns, error
✗ Incorrect
Keys become columns: 'City' and 'Population'.
Explain how to create a pandas DataFrame from a dictionary and what the dictionary keys and values represent.
Think about how a table is formed from columns and rows.
You got /4 concepts.
Describe what happens if the dictionary values have different lengths when creating a DataFrame.
Consider the shape of a table and why it must be consistent.
You got /3 concepts.
Practice
(1/5)
1. What happens when you create a pandas DataFrame from a dictionary where keys are column names and values are lists of equal length?
easy
A. The DataFrame will have rows named after the keys and columns from the list values.
B. A DataFrame is created with columns named after the keys and rows from the list values.
C. An error occurs because dictionaries cannot be converted to DataFrames.
D. Only the first key-value pair is used to create the DataFrame.
Solution
Step 1: Understand dictionary to DataFrame mapping
In pandas, dictionary keys become column names and values (lists) become column data.
Step 2: Check list lengths
All lists must have the same length to form rows correctly.
Final Answer:
A DataFrame is created with columns named after the keys and rows from the list values. -> Option B
Quick Check:
Dictionary keys = columns, lists = rows [OK]
Hint: Keys become columns, lists become rows if lengths match [OK]
Common Mistakes:
Thinking dictionary keys become row labels
Assuming only one key-value pair is used
Believing dictionaries can't create DataFrames
2. Which of the following is the correct syntax to create a pandas DataFrame from a dictionary named data?
easy
A. df = pd.DataFrame(data)
B. df = pd.DataFrame.from_dict(data, orient='index')
C. df = pd.DataFrame(data, columns=['A', 'B'])
D. df = pd.DataFrame(data).to_dict()
Solution
Step 1: Recall basic DataFrame creation syntax
The standard way to create a DataFrame from a dictionary is using pd.DataFrame(data).
Step 2: Analyze other options
df = pd.DataFrame.from_dict(data, orient='index') changes orientation to index, which is not default; df = pd.DataFrame(data, columns=['A', 'B']) tries to specify columns but may cause errors if columns don't match; df = pd.DataFrame(data).to_dict() converts DataFrame back to dict, not creation.
Final Answer:
df = pd.DataFrame(data) -> Option A
Quick Check:
Use pd.DataFrame(dictionary) to create DataFrame [OK]
Hint: Use pd.DataFrame(your_dict) for quick DataFrame creation [OK]
Common Mistakes:
Using .to_dict() instead of creating DataFrame
Confusing orient parameter for simple dict
Specifying columns that don't exist in dict
3. What will be the output of the following code?
import pandas as pd
data = {'Name': ['Alice', 'Bob'], 'Age': [25, 30]}
df = pd.DataFrame(data)
print(df)
medium
A. A DataFrame with rows named 'Name' and 'Age' and columns 0 and 1.
B. A dictionary printed with keys 'Name' and 'Age'.
C. An error because lists have different lengths.
D. A DataFrame with columns 'Name' and 'Age' and two rows with values Alice-25 and Bob-30.
Solution
Step 1: Create DataFrame from dictionary
The dictionary keys become columns 'Name' and 'Age'. The lists provide row data.
Step 2: Print DataFrame
Output shows two rows: first with 'Alice' and 25, second with 'Bob' and 30.
Final Answer:
A DataFrame with columns 'Name' and 'Age' and two rows with values Alice-25 and Bob-30. -> Option D