Bird
Raised Fist0
NumPydata~20 mins

NumPy with Pandas integration - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
NumPy-Pandas Integration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of NumPy array used in a Pandas DataFrame
What is the output of the following code snippet?
NumPy
import numpy as np
import pandas as pd

arr = np.array([10, 20, 30, 40])
df = pd.DataFrame({'values': arr})
print(df['values'] * 2)
A
0    20
1    40
2    60
3    80
Name: values, dtype: int64
B[20 40 60 80]
C[10 20 30 40]
DTypeError: unsupported operand type(s) for *: 'DataFrame' and 'int'
Attempts:
2 left
💡 Hint
Remember that multiplying a Pandas Series by a number multiplies each element.
❓ data_output
intermediate
1:30remaining
Shape of NumPy array extracted from DataFrame
Given the code below, what is the shape of the NumPy array 'arr'?
NumPy
import pandas as pd
import numpy as np

df = pd.DataFrame({'A': range(3), 'B': range(3, 6)})
arr = df.to_numpy()
A(3, 2)
B(2, 3)
C(3,)
D(2,)
Attempts:
2 left
💡 Hint
Check how many rows and columns the DataFrame has.
❓ visualization
advanced
2:30remaining
Visualizing correlation matrix using NumPy and Pandas
Which option correctly produces a heatmap visualization of the correlation matrix of the DataFrame 'df' using NumPy and Pandas?
NumPy
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

df = pd.DataFrame({
    'X': np.random.randn(100),
    'Y': np.random.randn(100),
    'Z': np.random.randn(100)
})
A
corr = df.corr()
plt.plot(corr)
plt.show()
B
corr = np.corrcoef(df)
plt.imshow(corr, cmap='coolwarm')
plt.colorbar()
plt.show()
C
corr = df.corr().values
plt.scatter(corr)
plt.show()
D
corr = df.corr().to_numpy()
plt.imshow(corr, cmap='coolwarm')
plt.colorbar()
plt.show()
Attempts:
2 left
💡 Hint
Use the DataFrame's built-in correlation method and a heatmap for visualization.
🔧 Debug
advanced
2:00remaining
Identify the error in NumPy array assignment from DataFrame
What error will the following code produce?
NumPy
import pandas as pd
import numpy as np

df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
arr = np.array(df['A', 'B'])
AIndexError: too many indices for array
BKeyError: ('A', 'B')
CNo error, arr is a NumPy array with columns A and B
DTypeError: unhashable type: 'slice'
Attempts:
2 left
💡 Hint
Check how columns are accessed in a DataFrame.
🚀 Application
expert
3:00remaining
Efficiently replace NaN values in DataFrame using NumPy
You have a DataFrame 'df' with some NaN values. Which option efficiently replaces all NaNs with the mean of their respective columns using NumPy?
NumPy
import pandas as pd
import numpy as np

df = pd.DataFrame({
    'A': [1, np.nan, 3],
    'B': [4, 5, np.nan]
})
A
for col in df.columns:
    df[col].fillna(df[col].mean(), inplace=True)
Bdf.fillna(df.mean(), inplace=True)
C
arr = df.to_numpy()
col_means = np.nanmean(arr, axis=0)
inds = np.where(np.isnan(arr))
arr[inds] = np.take(col_means, inds[1])
df.loc[:, :] = arr
D
arr = df.values
arr[np.isnan(arr)] = 0
df[:] = arr
Attempts:
2 left
💡 Hint
Use NumPy functions to compute means and replace NaNs in the array, then assign back.

Practice

(1/5)
1. What does the to_numpy() method do when used on a Pandas DataFrame column?
easy
A. Converts the column to a Pandas Series
B. Converts the column to a Python list
C. Converts the column to a NumPy array
D. Converts the column to a dictionary

Solution

  1. Step 1: Understand the method purpose

    The to_numpy() method is designed to convert Pandas data structures into NumPy arrays.
  2. Step 2: Apply to a DataFrame column

    When called on a DataFrame column, it returns the data as a NumPy array, which is useful for numerical operations.
  3. Final Answer:

    Converts the column to a NumPy array -> Option C
  4. Quick Check:

    to_numpy() = NumPy array [OK]
Hint: Remember: to_numpy() always returns a NumPy array [OK]
Common Mistakes:
  • Confusing to_numpy() with to_list()
  • Thinking it returns a Pandas Series
  • Assuming it converts to a dictionary
2. Which of the following is the correct syntax to convert a Pandas DataFrame column named 'age' to a NumPy array?
easy
A. df['age'].to_numpy()
B. np.to_numpy(df['age'])
C. df.to_numpy('age')
D. df.age.numpy()

Solution

  1. Step 1: Identify the correct method call

    The to_numpy() method is called on a Pandas Series or DataFrame column, so df['age'].to_numpy() is correct.
  2. Step 2: Check other options for errors

    np.to_numpy() does not exist, df.to_numpy('age') is invalid syntax, and df.age.numpy() is not a valid method.
  3. Final Answer:

    df['age'].to_numpy() -> Option A
  4. Quick Check:

    Correct method call = df['age'].to_numpy() [OK]
Hint: Use to_numpy() directly on the DataFrame column [OK]
Common Mistakes:
  • Trying to call to_numpy() from numpy module
  • Passing column name as argument to to_numpy()
  • Using .numpy() instead of .to_numpy()
3. Given the code below, what is the output of result?
import pandas as pd
import numpy as np
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
arr = df['A'].to_numpy()
result = np.sum(arr * 2)
medium
A. 36
B. 12
C. 21
D. 18

Solution

  1. Step 1: Convert column 'A' to NumPy array

    df['A'] is [1, 2, 3], so arr = [1, 2, 3].
  2. Step 2: Multiply array by 2 and sum

    arr * 2 = [2, 4, 6]. Sum is 2 + 4 + 6 = 12.
  3. Step 3: Match to options

    The calculated sum is 12, which matches 12.
  4. Final Answer:

    12 -> Option B
  5. Quick Check:

    Sum of doubled array = 12 [OK]
Hint: Multiply array first, then sum to avoid confusion [OK]
Common Mistakes:
  • Summing before multiplying
  • Confusing DataFrame with NumPy array
  • Misreading the multiplication factor
4. Identify the error in the following code snippet:
import pandas as pd
import numpy as np
df = pd.DataFrame({'X': [10, 20, 30]})
arr = df.X.to_numpy
print(np.mean(arr))
medium
A. Missing parentheses after to_numpy
B. DataFrame column 'X' does not exist
C. np.mean cannot be applied to NumPy arrays
D. print statement syntax is incorrect

Solution

  1. Step 1: Check method call syntax

    The code uses df.X.to_numpy without parentheses, so it assigns the method itself, not the array.
  2. Step 2: Understand the impact

    Passing a method object to np.mean() causes an error because it's not an array.
  3. Final Answer:

    Missing parentheses after to_numpy -> Option A
  4. Quick Check:

    Method call needs () = Missing parentheses after to_numpy [OK]
Hint: Always add () to call methods like to_numpy() [OK]
Common Mistakes:
  • Forgetting parentheses on method calls
  • Assuming attribute access returns data
  • Misunderstanding error messages
5. You have a Pandas DataFrame df with columns 'height' and 'weight'. You want to create a new NumPy array BMI using the formula weight / (height/100)**2. Which code correctly uses NumPy with Pandas to compute BMI?
hard
A. bmi = df['weight'].tolist() / np.array(df['height'] / 100) ** 2
B. bmi = df['weight'].divide((df['height'] / 100) ** 2)
C. bmi = df['weight'].to_numpy() / (df['height'].to_numpy() / 100) ** 2
D. bmi = df['weight'] / (df['height'] / 100) ** 2

Solution

  1. Step 1: Understand Pandas operations

    bmi = df['weight'] / (df['height'] / 100) ** 2 uses Pandas Series operations which support element-wise division and return a Series, which can be used as a NumPy array if needed.
  2. Step 2: Check other options

    bmi = df['weight'].divide((df['height'] / 100) ** 2) uses .divide(), which is similar to bmi = df['weight'] / (df['height'] / 100) ** 2 and returns a Series. bmi = df['weight'].to_numpy() / (df['height'].to_numpy() / 100) ** 2 converts to NumPy arrays explicitly, which is correct but not necessary. bmi = df['weight'].tolist() / np.array(df['height'] / 100) ** 2 uses .tolist() which returns a list and will cause a TypeError when dividing by a NumPy array.
  3. Step 3: Choose the best option

    bmi = df['weight'] / (df['height'] / 100) ** 2 is the most straightforward and idiomatic Pandas way to compute BMI, returning a Series that behaves like a NumPy array for most purposes.
  4. Final Answer:

    bmi = df['weight'] / (df['height'] / 100) ** 2 -> Option D
  5. Quick Check:

    Use Pandas Series operations for element-wise math [OK]
Hint: Pandas Series support element-wise math without conversion [OK]
Common Mistakes:
  • Mixing Pandas Series and NumPy arrays without conversion
  • Applying np.array incorrectly on expressions
  • Ignoring unit conversion (height/100)
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