Challenge - 5 Problems
DataFrame Dictionary Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
Think about how pandas uses dictionary keys as column names and list values as column data.
✗ Incorrect
When creating a DataFrame from a dictionary of lists, keys become column names and lists become column data. Index defaults to 0,1,...
❓ data_output
intermediate2: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))
Attempts:
2 left
💡 Hint
Check if pandas allows columns with different lengths when creating DataFrame.
✗ Incorrect
Pandas requires all columns to have the same length when creating a DataFrame from a dictionary of lists. Otherwise, it raises a ValueError.
❓ visualization
advanced2: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)
Attempts:
2 left
💡 Hint
Nested dictionaries create DataFrame with outer keys as columns and inner keys as index.
✗ Incorrect
When creating a DataFrame from a nested dictionary, outer keys become columns and inner keys become row indices.
🧠 Conceptual
advanced2:00remaining
Effect of dictionary keys on DataFrame columns
If you create a DataFrame from this dictionary:
What will be the column names of the DataFrame?
{0: ['x', 'y'], 1: ['a', 'b']}What will be the column names of the DataFrame?
Attempts:
2 left
💡 Hint
Dictionary keys become column names directly, preserving their type.
✗ Incorrect
The keys 0 and 1 become column names as integers, not strings or values.
🔧 Debug
expert2: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)Attempts:
2 left
💡 Hint
Pandas broadcasts scalar values to match the length of list columns.
✗ Incorrect
When dictionary values mix lists and scalars, pandas broadcasts the scalar value to fill the column, matching the list length.