Challenge - 5 Problems
NumPy to DataFrame Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this code creating a DataFrame?
Consider the following code that creates a DataFrame from a NumPy array. What will be the output DataFrame?
Pandas
import numpy as np import pandas as pd arr = np.array([[1, 2], [3, 4]]) df = pd.DataFrame(arr, columns=['A', 'B']) print(df)
Attempts:
2 left
💡 Hint
Look at how the columns parameter names the columns and the default index.
✗ Incorrect
The DataFrame is created with columns named 'A' and 'B'. The default index is 0 and 1. The values match the array rows.
❓ data_output
intermediate1:30remaining
How many rows and columns does this DataFrame have?
Given this code creating a DataFrame from a NumPy array, how many rows and columns does the resulting DataFrame have?
Pandas
import numpy as np import pandas as pd arr = np.arange(12).reshape(3, 4) df = pd.DataFrame(arr) print(df.shape)
Attempts:
2 left
💡 Hint
The reshape method sets rows and columns as (3, 4).
✗ Incorrect
The array is reshaped to 3 rows and 4 columns, so the DataFrame shape is (3, 4).
🔧 Debug
advanced2:00remaining
What error does this code raise?
This code tries to create a DataFrame from a NumPy array with mismatched columns. What error will it raise?
Pandas
import numpy as np import pandas as pd arr = np.array([[1, 2], [3, 4]]) df = pd.DataFrame(arr, columns=['A', 'B', 'C'])
Attempts:
2 left
💡 Hint
Check if the number of columns matches the array shape.
✗ Incorrect
The columns list has 3 names but the array has only 2 columns, causing a ValueError.
🚀 Application
advanced2:30remaining
Which option produces a DataFrame with custom row labels?
You want to create a DataFrame from a NumPy array with columns 'X', 'Y' and row labels 'a', 'b'. Which code does this correctly?
Pandas
import numpy as np import pandas as pd arr = np.array([[10, 20], [30, 40]])
Attempts:
2 left
💡 Hint
Columns are the names of the data columns, index is for row labels.
✗ Incorrect
Option C correctly assigns columns and index labels matching the data shape.
🧠 Conceptual
expert3:00remaining
What happens if you create a DataFrame from a 3D NumPy array?
You try to create a DataFrame from a 3-dimensional NumPy array. What will happen?
Pandas
import numpy as np import pandas as pd arr = np.ones((2, 2, 2)) df = pd.DataFrame(arr)
Attempts:
2 left
💡 Hint
DataFrames require 2D data structures.
✗ Incorrect
Pandas DataFrame constructor only accepts 1D or 2D arrays. 3D arrays cause a ValueError.