0
0
Pandasdata~20 mins

Creating DataFrame from dictionary in Pandas - Practice Exercises

Choose your learning style9 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.