Bird
Raised Fist0
Pandasdata~20 mins

Creating DataFrame from dictionary in Pandas - Practice Exercises

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
DataFrame Dictionary Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of DataFrame from dictionary with lists
What is the output of this code?
import pandas as pd
data = {'A': [1, 2], 'B': [3, 4]}
df = pd.DataFrame(data)
print(df)
Pandas
import pandas as pd
data = {'A': [1, 2], 'B': [3, 4]}
df = pd.DataFrame(data)
print(df)
A
   A  B
0  1  3
1  2  4
B
   A  B
1  1  3
2  2  4
CError: Length mismatch between columns and data
D
   0  1
A  1  2
B  3  4
Attempts:
2 left
💡 Hint
Think about how pandas uses dictionary keys as column names and list values as column data.
❓ data_output
intermediate
2:00remaining
Number of rows in DataFrame from dictionary with unequal list lengths
Given this code, how many rows will the DataFrame have?
import pandas as pd
data = {'X': [10, 20, 30], 'Y': [5, 15]}
df = pd.DataFrame(data)
print(len(df))
Pandas
import pandas as pd
data = {'X': [10, 20, 30], 'Y': [5, 15]}
df = pd.DataFrame(data)
print(len(df))
AError: All arrays must be of the same length
B2
C3
D1
Attempts:
2 left
💡 Hint
Check if pandas allows columns with different lengths when creating DataFrame.
❓ visualization
advanced
2:00remaining
Visualizing DataFrame created from nested dictionary
What does the DataFrame look like after running this code?
import pandas as pd
data = {'one': {'a': 1, 'b': 2}, 'two': {'a': 3, 'b': 4}}
df = pd.DataFrame(data)
print(df)
Pandas
import pandas as pd
data = {'one': {'a': 1, 'b': 2}, 'two': {'a': 3, 'b': 4}}
df = pd.DataFrame(data)
print(df)
AError: Cannot create DataFrame from nested dictionary
B
   a  b
one 1  2
two 3  4
C
   one  two
0    1    3
1    2    4
D
   one  two
 a    1    3
 b    2    4
Attempts:
2 left
💡 Hint
Nested dictionaries create DataFrame with outer keys as columns and inner keys as index.
🧠 Conceptual
advanced
2:00remaining
Effect of dictionary keys on DataFrame columns
If you create a DataFrame from this dictionary:
{0: ['x', 'y'], 1: ['a', 'b']}

What will be the column names of the DataFrame?
A['a', 'b']
B['x', 'y']
C[0, 1]
D['0', '1']
Attempts:
2 left
💡 Hint
Dictionary keys become column names directly, preserving their type.
🔧 Debug
expert
2:00remaining
DataFrame from dictionary with mixed list and scalar values
What happens when running this code?
import pandas as pd
data = {'col1': [1, 2], 'col2': 3}
df = pd.DataFrame(data)
ATypeError: DataFrame constructor not supported for this input
BNo error, DataFrame created with col2 filled with 3
CValueError: If using all scalar values, you must pass index
DKeyError: 'col2'
Attempts:
2 left
💡 Hint
Pandas broadcasts scalar values to match the length of list columns.

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

  1. Step 1: Understand dictionary to DataFrame mapping

    In pandas, dictionary keys become column names and values (lists) become column data.
  2. Step 2: Check list lengths

    All lists must have the same length to form rows correctly.
  3. Final Answer:

    A DataFrame is created with columns named after the keys and rows from the list values. -> Option B
  4. 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

  1. Step 1: Recall basic DataFrame creation syntax

    The standard way to create a DataFrame from a dictionary is using pd.DataFrame(data).
  2. 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.
  3. Final Answer:

    df = pd.DataFrame(data) -> Option A
  4. 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

  1. Step 1: Create DataFrame from dictionary

    The dictionary keys become columns 'Name' and 'Age'. The lists provide row data.
  2. Step 2: Print DataFrame

    Output shows two rows: first with 'Alice' and 25, second with 'Bob' and 30.
  3. Final Answer:

    A DataFrame with columns 'Name' and 'Age' and two rows with values Alice-25 and Bob-30. -> Option D
  4. Quick Check:

    Dictionary keys = columns, lists = rows [OK]
Hint: Keys = columns, lists = rows; print shows table [OK]
Common Mistakes:
  • Expecting dictionary print instead of DataFrame
  • Confusing rows and columns
  • Assuming error due to list lengths
4. Identify the error in this code snippet that tries to create a DataFrame from a dictionary:
import pandas as pd
data = {'A': [1, 2], 'B': [3]}
df = pd.DataFrame(data)
print(df)
medium
A. Lists in the dictionary have different lengths causing a ValueError.
B. The dictionary keys are not strings.
C. pd.DataFrame() cannot take a dictionary as input.
D. The print statement is missing parentheses.

Solution

  1. Step 1: Check list lengths in dictionary

    List under key 'A' has length 2, but list under 'B' has length 1.
  2. Step 2: Understand DataFrame requirements

    All lists must be the same length to form rows; mismatch causes ValueError.
  3. Final Answer:

    Lists in the dictionary have different lengths causing a ValueError. -> Option A
  4. Quick Check:

    All lists must be equal length [OK]
Hint: Check all lists have same length before DataFrame creation [OK]
Common Mistakes:
  • Ignoring list length mismatch
  • Thinking keys must be non-string
  • Assuming print syntax error in Python 3
5. You have a dictionary with some keys having empty lists and others with data:
data = {'X': [1, 2, 3], 'Y': [], 'Z': [4, 5, 6]}

How can you create a valid DataFrame from this dictionary?
hard
A. Remove keys with empty lists before creating the DataFrame.
B. Create the DataFrame directly; pandas fills missing columns with NaN.
C. Replace empty lists with lists of None matching other list lengths.
D. Use pd.DataFrame.from_dict(data, orient='index') to fix empty lists.

Solution

  1. Step 1: Identify problem with empty lists

    Empty list for key 'Y' has length 0, others have length 3, causing mismatch.
  2. Step 2: Fix by replacing empty lists

    Replace empty list with [None, None, None] to match lengths and keep column.
  3. Step 3: Create DataFrame

    Now all lists have equal length; DataFrame creation succeeds with NaN or None in 'Y'.
  4. Final Answer:

    Replace empty lists with lists of None matching other list lengths. -> Option C
  5. Quick Check:

    All columns must have equal-length lists [OK]
Hint: Replace empty lists with None-filled lists of correct length [OK]
Common Mistakes:
  • Trying to create DataFrame directly with empty lists
  • Removing keys losing important data
  • Misusing orient parameter for this issue
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard