Bird
Raised Fist0
Pandasdata~20 mins

Creating DataFrame from NumPy array in Pandas - Practice Exercises

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
NumPy to DataFrame Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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)
A
   A  B
1  1  2
2  3  4
B
   A  B
0  1  2
1  3  4
C
   A  B
0  2  1
1  4  3
D
   0  1
0  1  2
1  3  4
Attempts:
2 left
💡 Hint
Look at how the columns parameter names the columns and the default index.
❓ data_output
intermediate
1: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)
A(3, 4)
B(4, 3)
C(12, 1)
D(1, 12)
Attempts:
2 left
💡 Hint
The reshape method sets rows and columns as (3, 4).
🔧 Debug
advanced
2: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'])
ANo error, DataFrame created with NaN in missing column
BTypeError: columns must be a list of integers
CIndexError: list index out of range
DValueError: 3 columns passed, passed data had 2 columns
Attempts:
2 left
💡 Hint
Check if the number of columns matches the array shape.
🚀 Application
advanced
2: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]])
Apd.DataFrame(arr, index=['X', 'Y'], columns=['a', 'b'])
Bpd.DataFrame(arr, columns=['a', 'b'], index=['X', 'Y'])
Cpd.DataFrame(arr, columns=['X', 'Y'], index=['a', 'b'])
Dpd.DataFrame(arr, index=['a', 'b'])
Attempts:
2 left
💡 Hint
Columns are the names of the data columns, index is for row labels.
🧠 Conceptual
expert
3: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)
ARaises ValueError: Must pass 2-d input
BCreates a DataFrame flattening the array automatically
CCreates a DataFrame with nested arrays in cells
DCreates a DataFrame with default integer columns and rows
Attempts:
2 left
💡 Hint
DataFrames require 2D data structures.

Practice

(1/5)
1. What does creating a pandas DataFrame from a NumPy array allow you to do?
easy
A. Automatically visualize the data as a chart
B. Convert a DataFrame back into a NumPy array automatically
C. Organize array data into a labeled table with rows and columns
D. Run SQL queries directly on the NumPy array

Solution

  1. Step 1: Understand what a DataFrame is

    A DataFrame is a table-like structure with rows and columns, which helps organize data clearly.
  2. Step 2: Know the role of NumPy arrays

    NumPy arrays hold data in a grid but lack labels for rows and columns.
  3. Final Answer:

    Organize array data into a labeled table with rows and columns -> Option C
  4. Quick Check:

    DataFrame = labeled table from array [OK]
Hint: DataFrames add labels to arrays for easier data handling [OK]
Common Mistakes:
  • Thinking DataFrame converts arrays to charts automatically
  • Confusing DataFrame creation with SQL querying
  • Believing DataFrame auto-converts back to NumPy arrays
2. Which of the following is the correct way to create a pandas DataFrame from a NumPy array arr with column names ['A', 'B']?
easy
A. pd.DataFrame(arr, columns=['A', 'B'])
B. pd.DataFrame(arr, index=['A', 'B'])
C. pd.DataFrame(columns=['A', 'B'], arr)
D. pd.DataFrame(arr, names=['A', 'B'])

Solution

  1. Step 1: Recall DataFrame constructor parameters

    The columns parameter sets column labels, and data is the array.
  2. Step 2: Check each option

    pd.DataFrame(arr, columns=['A', 'B']) correctly uses columns=['A', 'B'] with the array as data. pd.DataFrame(arr, index=['A', 'B']) wrongly uses index for columns. pd.DataFrame(columns=['A', 'B'], arr) incorrectly passes arr positionally after keyword columns, causing a syntax error. pd.DataFrame(arr, names=['A', 'B']) uses a non-existent names parameter.
  3. Final Answer:

    pd.DataFrame(arr, columns=['A', 'B']) -> Option A
  4. Quick Check:

    Use columns= for column names [OK]
Hint: Use columns= to name columns when creating DataFrame [OK]
Common Mistakes:
  • Using index= instead of columns= for column names
  • Passing data positionally after columns keyword (syntax error)
  • Using invalid parameter names like names=
3. What will be the output of the following code?
import numpy as np
import pandas as pd
arr = np.array([[1, 2], [3, 4]])
df = pd.DataFrame(arr, columns=['X', 'Y'])
print(df)
medium
A. X Y 1 2 3 4 5 6
B. 0 1 X 1 2 Y 3 4
C. Error: columns length mismatch
D. X Y 0 1 2 1 3 4

Solution

  1. Step 1: Understand the array and DataFrame creation

    The array has shape (2,2) with values [[1,2],[3,4]]. Columns are named ['X', 'Y'].
  2. Step 2: Predict DataFrame output

    Rows get default index 0 and 1. Columns are X and Y. Values fill accordingly.
  3. Final Answer:

    X Y 0 1 2 1 3 4 -> Option D
  4. Quick Check:

    Array shape matches DataFrame shape with columns named [OK]
Hint: Rows get default 0-based index if not specified [OK]
Common Mistakes:
  • Confusing row and column labels
  • Expecting error due to columns parameter
  • Misreading array shape and values
4. Identify the error in this code snippet that tries to create a DataFrame from a NumPy array:
import numpy as np
import pandas as pd
arr = np.array([[5, 6], [7, 8]])
df = pd.DataFrame(arr, columns=['A'])
print(df)
medium
A. NumPy array must be 1D to create a DataFrame
B. The columns list length does not match the number of array columns
C. DataFrame constructor requires index parameter when columns are given
D. The array contains integers, which are not allowed in DataFrames

Solution

  1. Step 1: Check array shape and columns length

    The array has 2 columns, but columns=['A'] has length 1.
  2. Step 2: Understand pandas error

    pandas raises a ValueError because the number of columns does not match the array's second dimension.
  3. Final Answer:

    The columns list length does not match the number of array columns -> Option B
  4. Quick Check:

    Columns length must equal array columns [OK]
Hint: Columns list length must match array's column count [OK]
Common Mistakes:
  • Assuming 1D array is required
  • Thinking index parameter is mandatory with columns
  • Believing integers are invalid in DataFrames
5. You have a NumPy array arr = np.array([[10, 20], [30, 40], [50, 60]]). You want to create a DataFrame with columns named ['Height', 'Weight'] and row labels ['Alice', 'Bob', 'Charlie']. Which code correctly creates this DataFrame?
hard
A. pd.DataFrame(arr, columns=['Height', 'Weight'], index=['Alice', 'Bob', 'Charlie'])
B. pd.DataFrame(arr, index=['Height', 'Weight'], columns=['Alice', 'Bob', 'Charlie'])
C. pd.DataFrame(data=arr, names=['Height', 'Weight'], labels=['Alice', 'Bob', 'Charlie'])
D. pd.DataFrame(arr, rows=['Alice', 'Bob', 'Charlie'], cols=['Height', 'Weight'])

Solution

  1. Step 1: Understand DataFrame parameters for rows and columns

    Use columns= to name columns and index= to name rows.
  2. Step 2: Check each option

    pd.DataFrame(arr, columns=['Height', 'Weight'], index=['Alice', 'Bob', 'Charlie']) correctly uses columns=['Height', 'Weight'] and index=['Alice', 'Bob', 'Charlie']. pd.DataFrame(arr, index=['Height', 'Weight'], columns=['Alice', 'Bob', 'Charlie']) swaps index and columns incorrectly. pd.DataFrame(data=arr, names=['Height', 'Weight'], labels=['Alice', 'Bob', 'Charlie']) uses invalid parameters names and labels. pd.DataFrame(arr, rows=['Alice', 'Bob', 'Charlie'], cols=['Height', 'Weight']) uses non-existent rows and cols parameters.
  3. Final Answer:

    pd.DataFrame(arr, columns=['Height', 'Weight'], index=['Alice', 'Bob', 'Charlie']) -> Option A
  4. Quick Check:

    Use columns= for columns, index= for rows [OK]
Hint: Use index= for rows, columns= for columns when creating DataFrame [OK]
Common Mistakes:
  • Swapping index and columns parameters
  • Using invalid parameter names like rows= or names=
  • Forgetting to match index length to array rows
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