Challenge - 5 Problems
DataFrame Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this DataFrame creation code?
Given the code below, what will be the content of the DataFrame
df?Pandas
import pandas as pd data = [[1, 2], [3, 4], [5, 6]] df = pd.DataFrame(data, columns=['A', 'B']) print(df)
Attempts:
2 left
💡 Hint
Check how columns are assigned when creating a DataFrame from a list of lists.
✗ Incorrect
The DataFrame is created with two columns named 'A' and 'B'. Each inner list becomes a row. So the first row is [1, 2], second is [3, 4], and third is [5, 6].
❓ data_output
intermediate1:30remaining
How many rows and columns does this DataFrame have?
Consider this code creating a DataFrame from a list of lists. How many rows and columns does
df have?Pandas
import pandas as pd data = [[10, 20, 30], [40, 50, 60]] df = pd.DataFrame(data) print(df.shape)
Attempts:
2 left
💡 Hint
Each inner list is a row; count rows and columns accordingly.
✗ Incorrect
There are 2 inner lists, so 2 rows. Each inner list has 3 elements, so 3 columns. The shape is (2, 3).
🔧 Debug
advanced2:00remaining
Why does this DataFrame creation code raise an error?
Look at this code. Why does it raise an error?
Pandas
import pandas as pd data = [[1, 2], [3, 4]] df = pd.DataFrame(data, columns=['A'])
Attempts:
2 left
💡 Hint
Check if the number of columns matches the number of elements in each inner list.
✗ Incorrect
The data has 2 elements per row but only 1 column name is given. This mismatch causes a ValueError.
🚀 Application
advanced2:30remaining
Create a DataFrame with custom row labels from list of lists
You have this list of lists:
data = [[7, 8], [9, 10]]. You want to create a DataFrame with columns named X and Y and row labels row1 and row2. Which code does this correctly?Attempts:
2 left
💡 Hint
Remember the parameters for columns and index in DataFrame constructor.
✗ Incorrect
The columns parameter sets column names, index sets row labels. Option A uses them correctly.
🧠 Conceptual
expert3:00remaining
What happens if inner lists have different lengths when creating a DataFrame?
Consider this code:
import pandas as pd data = [[1, 2], [3, 4, 5]] df = pd.DataFrame(data)What will be the content of
df?Attempts:
2 left
💡 Hint
Pandas fills missing values with NaN when rows have different lengths.
✗ Incorrect
Pandas creates columns for the longest row length and fills missing values with NaN for shorter rows.