Scientific Computing with NumPy and Pandas - Exam Questions
2-Marks Questions (Answer Any 5):
1. What is the difference between a NumPy array and a Python list?
Ans: NumPy arrays are more efficient, support vectorized operations, and consume less memory than Python
lists.
2. Define broadcasting in NumPy with a simple example.
Ans: Broadcasting allows NumPy to perform arithmetic operations on arrays of different shapes. E.g., adding
a scalar to a 2D array.
3. How do you generate a 1D array of 10 random integers between 1 and 100 in NumPy?
Ans: Using: np.random.randint(1, 101, size=10)
4. What is the purpose of the axis parameter in NumPy aggregation functions?
Ans: The axis parameter determines the dimension along which the function operates. axis=0 for columns,
axis=1 for rows.
5. Write code to select values greater than 10 from a NumPy array arr.
Ans: arr[arr > 10]
6. How is a Pandas Series different from a DataFrame?
Ans: A Series is a 1D labeled array, while a DataFrame is a 2D labeled table with columns.
7. Mention two functions to load data into a Pandas DataFrame.
Ans: read_csv(), read_excel()
5-Marks Questions (Answer Any 5):
1. Explain array indexing in NumPy with examples.
Ans: Array indexing allows accessing or modifying specific elements. E.g., arr[1], arr[2:5], arr[:,1].
2. Write Python code to perform element-wise addition, multiplication, and division of two NumPy arrays.
Ans: import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
Addition: arr1 + arr2
Multiplication: arr1 * arr2
Division: arr1 / arr2
3. Discuss NumPy's mathematical and statistical functions with examples.
Ans: Functions include np.mean(), np.std(), np.sum(), np.min(), np.max().
E.g., np.mean([1,2,3]) returns 2.0
4. How do you generate random numbers in NumPy? Explain with code.
Ans: Using np.random module. Examples:
np.random.rand(3)
np.random.randint(1, 10, 5)
np.random.normal(0, 1, 3)
5. What is a Pandas Series? Write code to create and access elements of a Series.
Ans: A Series is a one-dimensional labeled array.
import pandas as pd
s = pd.Series([10, 20, 30], index=['a', 'b', 'c'])
s['b'] returns 20
6. Explain the process of filtering and selecting data in Pandas with code.
Ans: df[df['age'] > 25] selects rows where age > 25
df[['name', 'age']] selects specific columns.
7. Describe groupby and aggregation in Pandas with an example.
Ans: groupby() splits the data and apply aggregation like mean(), sum().
E.g., df.groupby('dept')['salary'].mean()