What is the relationship between Pandas and NumPy?
Pandas is built on top of NumPy. It uses NumPy arrays to store data internally, which allows fast numerical operations and efficient memory use.
Click to reveal answer
beginner
How can you convert a Pandas DataFrame column to a NumPy array?
You can use the .to_numpy() method on a DataFrame column to get a NumPy array of its values.
Click to reveal answer
intermediate
Why is NumPy important for Pandas performance?
NumPy provides fast, vectorized operations on arrays. Pandas uses these to perform calculations quickly on large datasets without slow Python loops.
Click to reveal answer
intermediate
How do Pandas and NumPy handle missing data differently?
NumPy uses np.nan to represent missing values in float arrays. Pandas extends this by supporting NaN in numeric columns and None or pd.NA in other types.
Click to reveal answer
beginner
Show a simple example of creating a Pandas Series from a NumPy array.
Example:<br><pre>import numpy as np
import pandas as pd
arr = np.array([10, 20, 30])
series = pd.Series(arr)
print(series)</pre>
Click to reveal answer
Which method converts a Pandas DataFrame column to a NumPy array?
A.array()
B.to_list()
C.asarray()
D.to_numpy()
✗ Incorrect
The correct method is .to_numpy() which returns the column as a NumPy array.
What does Pandas use internally to store data?
APython lists
BNumPy arrays
CDictionaries
DTuples
✗ Incorrect
Pandas stores data internally as NumPy arrays for efficient computation.
Which of these is a missing value marker in NumPy arrays?
ANaN
BNone
Cpd.NA
D0
✗ Incorrect
NumPy uses np.nan (Not a Number) to represent missing values in float arrays.
Why does Pandas rely on NumPy for performance?
ANumPy uses less memory
BNumPy has faster loops
CNumPy provides vectorized operations
DNumPy supports strings better
✗ Incorrect
NumPy provides vectorized operations that allow fast calculations without slow Python loops.
Which of these is NOT a way Pandas handles missing data?
A0
Bpd.NA
Cnp.nan
DNone
✗ Incorrect
Zero (0) is a valid number, not a missing data marker.
Explain how Pandas and NumPy work together to handle data efficiently.
Think about how Pandas builds on NumPy's strengths.
You got /4 concepts.
Describe how to convert data between Pandas and NumPy formats and why you might do this.
Consider when you want to use NumPy functions on Pandas data.
You got /4 concepts.
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
Step 1: Understand Pandas data storage
Pandas stores data in tables called DataFrames, which need efficient storage for speed.
Step 2: Role of NumPy arrays
NumPy arrays provide fast, memory-efficient storage and operations on numerical data.
Final Answer:
To store data efficiently and allow fast computations -> Option C
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
Step 1: Recall Pandas method for conversion
Pandas DataFrames have a method to_numpy() to convert to NumPy arrays.
Step 2: Check other options
np.array(df) works but is not the dedicated Pandas method; df.numpy() and df.asarray() do not exist.
Final Answer:
df.to_numpy() -> Option B
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
Step 1: Understand np.sum on DataFrame
Applying np.sum to a DataFrame sums each column and returns a NumPy array.
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]).
Final Answer:
array([6, 15]) -> Option A
Quick Check:
NumPy sums columns into array [OK]
Hint: np.sum on DataFrame sums columns, returns NumPy array [OK]
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
Step 1: Check usage of df.values
df.values is an attribute, not a method, so parentheses cause an error.
Step 2: Understand addition to NumPy array
Adding 1 to a NumPy array works element-wise, so no error there.
Final Answer:
df.values() should be df.values without parentheses -> Option A
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
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.
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.
Final Answer:
new_df = df.apply(np.log) -> Option D
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