Bird
Raised Fist0
Pandasdata~20 mins

Pandas and NumPy connection - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Pandas and NumPy Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of NumPy array converted to Pandas Series
What is the output of this code snippet?
Pandas
import numpy as np
import pandas as pd
arr = np.array([10, 20, 30, 40])
series = pd.Series(arr)
print(series)
A[10 20 30 40]
B
0    10
1    20
2    30
3    40
dtype: int64
C
0    10
1    20
2    30
3    40
Name: arr, dtype: int64
DTypeError: cannot convert array to Series
Attempts:
2 left
💡 Hint
Think about how pandas.Series displays numpy arrays with default index.
❓ data_output
intermediate
1:30remaining
Shape of NumPy array from Pandas DataFrame values
What is the shape of the NumPy array obtained from the DataFrame values below?
Pandas
import pandas as pd
import numpy as np
df = pd.DataFrame({'A': [1, 2], 'B': [3, 4], 'C': [5, 6]})
arr = df.values
print(arr.shape)
A(6,)
B(3, 2)
C(2, 3)
D(3,)
Attempts:
2 left
💡 Hint
Remember rows and columns in DataFrame translate to shape (rows, columns).
🔧 Debug
advanced
2:00remaining
Error when assigning NumPy array to DataFrame column
What error does this code raise?
Pandas
import pandas as pd
import numpy as np
df = pd.DataFrame({'X': [1, 2, 3]})
arr = np.array([4, 5])
df['Y'] = arr
AValueError: Length of values (2) does not match length of index (3)
BTypeError: Cannot assign array of shape (2,) to column of shape (3,)
CKeyError: 'Y'
DNo error, DataFrame updated successfully
Attempts:
2 left
💡 Hint
Check if the length of the array matches the DataFrame rows.
❓ visualization
advanced
2:30remaining
Plotting NumPy data from Pandas DataFrame
Which option produces a line plot of the 'values' column from the DataFrame?
Pandas
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
data = {'values': np.array([1, 3, 2, 5, 4])}
df = pd.DataFrame(data)
# Plot code here
A
plt.plot(df.values)
plt.show()
B
plt.plot(np.array(df))
plt.show()
C
plt.plot(df['values'].values)
plt.show()
D
plt.plot(df['values'])
plt.show()
Attempts:
2 left
💡 Hint
Use the DataFrame column directly for plotting.
🧠 Conceptual
expert
2:30remaining
Effect of modifying NumPy array on Pandas DataFrame
Given this code, what is the value of df.loc[0, 'A'] after modifying arr[0]?
Pandas
import pandas as pd
import numpy as np
df = pd.DataFrame({'A': [10, 20, 30]})
arr = df.values
arr[0, 0] = 99
print(df.loc[0, 'A'])
A99
BNone
C10
DRaises ValueError
Attempts:
2 left
💡 Hint
Consider if df.values returns a view or a copy of the data.

Practice

(1/5)
1. What is the main reason Pandas uses NumPy arrays internally?
easy
A. To handle text data only
B. To make data visualization easier
C. To store data efficiently and allow fast computations
D. To create interactive dashboards

Solution

  1. Step 1: Understand Pandas data storage

    Pandas stores data in tables called DataFrames, which need efficient storage for speed.
  2. Step 2: Role of NumPy arrays

    NumPy arrays provide fast, memory-efficient storage and operations on numerical data.
  3. Final Answer:

    To store data efficiently and allow fast computations -> Option C
  4. Quick Check:

    Pandas uses NumPy arrays for efficiency [OK]
Hint: Pandas uses NumPy arrays for speed and memory [OK]
Common Mistakes:
  • Thinking Pandas uses NumPy only for visualization
  • Believing Pandas stores data as plain Python lists
  • Confusing NumPy arrays with text handling
2. Which of the following is the recommended way to convert a Pandas DataFrame df to a NumPy array?
easy
A. np.array(df)
B. df.to_numpy()
C. df.numpy()
D. df.asarray()

Solution

  1. Step 1: Recall Pandas method for conversion

    Pandas DataFrames have a method to_numpy() to convert to NumPy arrays.
  2. Step 2: Check other options

    np.array(df) works but is not the dedicated Pandas method; df.numpy() and df.asarray() do not exist.
  3. Final Answer:

    df.to_numpy() -> Option B
  4. Quick Check:

    Use to_numpy() to convert DataFrame [OK]
Hint: Use df.to_numpy() to get NumPy array from DataFrame [OK]
Common Mistakes:
  • Using non-existent methods like df.numpy()
  • Confusing np.array(df) as the best method
  • Trying to use df.asarray() which is invalid
3. Given the code:
import pandas as pd
import numpy as np
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
result = np.sum(df)

What is the value of result?
medium
A. array([6, 15])
B. [6 15]
C. 21
D. Error: cannot sum DataFrame with NumPy

Solution

  1. Step 1: Understand np.sum on DataFrame

    Applying np.sum to a DataFrame sums each column and returns a NumPy array.
  2. Step 2: Calculate sums of columns

    Column 'A' sums to 1+2+3=6, column 'B' sums to 4+5+6=15, so result is array([6, 15]).
  3. Final Answer:

    array([6, 15]) -> Option A
  4. Quick Check:

    NumPy sums columns into array [OK]
Hint: np.sum on DataFrame sums columns, returns NumPy array [OK]
Common Mistakes:
  • Expecting a single number sum of all values
  • Thinking np.sum causes error on DataFrame
  • Confusing list output with NumPy array
4. What is wrong with this code snippet?
import pandas as pd
import numpy as np
df = pd.DataFrame({'X': [1, 2], 'Y': [3, 4]})
arr = df.values()
print(arr + 1)
medium
A. df.values() should be df.values without parentheses
B. You cannot add 1 to a NumPy array
C. DataFrame does not have a values attribute
D. The print statement syntax is incorrect

Solution

  1. Step 1: Check usage of df.values

    df.values is an attribute, not a method, so parentheses cause an error.
  2. Step 2: Understand addition to NumPy array

    Adding 1 to a NumPy array works element-wise, so no error there.
  3. Final Answer:

    df.values() should be df.values without parentheses -> Option A
  4. Quick Check:

    Use df.values without () to get NumPy array [OK]
Hint: df.values is attribute, not method; no parentheses [OK]
Common Mistakes:
  • Calling df.values() as a method
  • Thinking addition to array is invalid
  • Assuming print syntax is wrong in Python 3
5. You have a Pandas DataFrame df with numeric columns. You want to apply the NumPy function np.log to all values and get a new DataFrame with the results preserving the original index and column names. Which code correctly does this?
hard
A. new_df = pd.DataFrame(np.log(df))
B. new_df = df.to_numpy().log()
C. new_df = np.log(df)
D. new_df = df.apply(np.log)

Solution

  1. Step 1: Understand applying NumPy functions on DataFrame

    Using df.apply(np.log) applies np.log element-wise to each column and returns a DataFrame preserving index and columns.
  2. Step 2: Check other options

    np.log(df) returns a NumPy array, not DataFrame; pd.DataFrame(np.log(df)) creates DataFrame but loses original index and column names; df.to_numpy().log() is invalid syntax.
  3. Final Answer:

    new_df = df.apply(np.log) -> Option D
  4. Quick Check:

    Use df.apply(np.log) for DataFrame output [OK]
Hint: Use df.apply(np.log) to keep DataFrame structure [OK]
Common Mistakes:
  • Using np.log(df) which returns array, not DataFrame
  • Trying df.to_numpy().log() which is invalid
  • Wrapping np.log(df) inside pd.DataFrame unnecessarily
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