NumPy MCQ : Set 6

Python NumPy MCQ

1). What is the output of the following code?

				
					import numpy as np
x = np.array([[4, 8, 0], [2, 0, 6]])
print("Number of non-zero elements: ", np.count_nonzero(x))
				
			

a) 4
b) 5
c) 6
d) 7

Correct answer is: a) 4
Explanation: The given code imports the NumPy library and creates a 2-dimensional NumPy array `x` with the elements [[4, 8, 0], [2, 0, 6]]. The `np.count_nonzero()` function is then used to count the number of non-zero elements in the array `x`.

2). What is the output of the following code?

				
					import numpy as np
x = np.array([[5, 3],
              [7, 4]])
print("Median of array:", np.median(x))
				
			

a) 3
b) 4
c) 5
d) 6

Correct answer is: b) 4.5
Explanation: The given code imports the NumPy library and creates a 2-dimensional NumPy array `x` with the elements [[5, 3], [7, 4]]. The `np.median()` function is then used to calculate the median of all elements in the array `x`.

3). What is the output of the following code?

				
					import numpy as np
x = np.array([[5, 3], [7, 4]])
print("Median of array along row:", np.median(x, axis=0))
				
			

a) [5, 3]
b) [7, 4]
c) [6, 3.5]
d) [4, 5]

Correct answer is: c) [6, 3.5]
Explanation: The given code imports the NumPy library and creates a 2-dimensional NumPy array `x` with the elements [[5, 3], [7, 4]]. The `np.median()` function is then used to calculate the median of the array along the rows, which means the median is computed for each column.

4). What is the output of the following code?

				
					import numpy as np
x = np.array([[5, 3], [7, 4]])
print("Median of array along column:", np.median(x, axis=1))
				
			

a) [4.0, 3.5]
b) [3.5, 4.0]
c) [3.0, 5.5]
d) [5.5, 3.0]

Correct answer is: a) [4.0, 5.5]
Explanation: The given code imports the NumPy library and creates a 2-dimensional NumPy array `x` with the elements [[5, 3], [7, 4]]. The `np.median()` function is then used to calculate the median along the column (axis 1) of the array `x`.

5). What is the purpose of the following code?

				
					import numpy as np
x = np.array([[4, 8, 0], [2, 0, 6]])

for ele in np.nditer(x):
    print(ele, end=' ')
				
			

a) It prints the elements of the 2-dimensional NumPy array `x`.
b) It calculates the sum of all elements in the 2-dimensional NumPy array `x`.
c) It reshapes the 2-dimensional NumPy array `x` to a 1-dimensional array.
d) It creates a new 2-dimensional array with the same elements as `x`.

Correct answer is: a) It prints the elements of the 2-dimensional NumPy array `x`.
Explanation: The given code imports the NumPy library and creates a 2-dimensional NumPy array `x` with the elements [[4, 8, 0], [2, 0, 6]]. It then uses a loop to iterate through the elements of the array `x` using the `np.nditer()` function. The `np.nditer()` function in NumPy allows iterating over all elements of an array regardless of its shape and dimensions. In the code, each element of the array `x` is printed using the loop with `print(ele, end=’ ‘)`.

6). What is the purpose of the following code?

				
					import numpy as np
x = np.array([2, 6, 3, 1])
print("Square of every element in the array: ", np.square(x))

				
			

a) It calculates the sum of all elements in the array `x`.
b) It calculates the mean of all elements in the array `x`.
c) It calculates the product of all elements in the array `x`.
d) It calculates the square of each element in the array `x`.

Correct answer is: d) It calculates the square of each element in the array `x`.
Explanation: The given code imports the NumPy library and creates a 1-dimensional NumPy array `x` with the elements [2, 6, 3, 1]. The `np.square()` function is then used to calculate the square of each element in the array `x`.

7). What is the output of the following code?

				
					import numpy as np
x = np.array([[4, 8, 0], [2, 0, 6]])
y = np.array([[5], [7]])

print(np.append(x, y, axis=1))
				
			

a) [[4, 8, 0], [2, 0, 6], [5], [7]]
b) [[4, 8, 0, 5], [2, 0, 6, 7]]
c) [[4, 5], [8, 7], [0, 0], [2, 6]]
d) [[4, 8, 0], [2, 0, 6], [5, 7]]

Correct answer is: b) [[4, 8, 0, 5], [2, 0, 6, 7]]
Explanation: The given code imports the NumPy library and creates two NumPy arrays `x` and `y`. The array `x` is a 2-dimensional array with the elements [[4, 8, 0], [2, 0, 6]]. The array `y` is a 2-dimensional array with the elements [[5], [7]]. The `np.append()` function is then used to append the array `y` to the array `x` along axis 1 (columns).

8). What is the output of the following code?

				
					import numpy as np
x = np.array([[4, 8, np.nan], [2, 4, 6]])
print("Old: \n", x)

print("New: \n", x[~np.isnan(x).any(axis=1)])
				
			

a) Old:
[[ 4. 8. nan]
[ 2. 4. 6.]]
New:
[[2. 4. 6.]]

b) Old:
[[ 4. 8. nan]
[ 2. 4. 6.]]
New:
[[4. 8. nan]]

c) Old:
[[ 4. 8. nan]
[ 2. 4. 6.]]
New:
[[4. 8. 6.]]

d) Old:
[[ 4. 8. nan]
[ 2. 4. 6.]]
New:
[[ 4. 8. nan]
[ 2. 4. 6.]]

Correct answer is: a) Old:
[[ 4. 8. nan]
[ 2. 4. 6.]]
New:
[[2. 4. 6.]]
Explanation: The given code uses NumPy to perform operations on a 2-dimensional array `x`, which contains some NaN (Not a Number) values. Let’s break down the code step by step. The code uses `np.isnan(x)` to create a boolean array, where `True` indicates the positions with NaN values in the original array. The `any(axis=1)` checks if there is any `True` in each row of the boolean array. The `~` is the logical NOT operator, which negates the boolean array. The modified array contains only the rows that do not have any NaN values. In this case, the row `[4, 8, nan]` is removed from the array, leaving only the row `[2, 4, 6]`.

9). What is the output of the following code?

				
					import numpy as np
x = np.array([3, 5, 1])
print("Magnitude of the given vector: ", np.linalg.norm(x))

				
			

a) 9
b) 3
c) 5.916079783099616
d) 35

Correct answer is: c) 5.916079783099616
Explanation: The given code imports the NumPy library and creates a 1-dimensional NumPy array `x` with the elements [3, 5, 1]. The `np.linalg.norm()` function is then used to calculate the magnitude (Euclidean norm) of the vector `x`. The magnitude of a vector is calculated as the square root of the sum of the squares of its elements.

10). What is the output of the following code?

				
					import numpy as np
x = np.array([8, 6, 7, 0, 7, 8, 6, 6])

unique, count = np.unique(x, return_counts=True)
print("Frequency of unique values in the given array:")
print(np.asarray((unique, count)))
				
			

a) [6, 7, 8, 0] [3, 2, 2, 1]
b) [0, 6, 7, 8] [1, 3, 2, 2]
c) [0, 6, 7, 8] [2, 3, 2, 1]
d) [0, 6, 7, 8] [3, 2, 1, 2]

Correct answer is: b) [0, 6, 7, 8] [1, 3, 2, 2]
Explanation: The given code imports the NumPy library and creates a 1-dimensional NumPy array `x` with the elements [8, 6, 7, 0, 7, 8, 6, 6]. The `np.unique()` function is then used with the parameter `return_counts=True` to find the unique values and their corresponding frequencies in the array `x`.

11). What is the output of the following code?

				
					import numpy as np
x = np.array([4, 8, 0])

if x.size == 0:
    print("Array is empty")
else:
    print("Array is not empty")

				
			

a) Array is empty
b) Array is not empty
c) 4 8 0
d) 3

Correct answer is: b) Array is not empty
Explanation: The given code imports the NumPy library and creates a 1-dimensional NumPy array `x` with the elements [4, 8, 0]. The code then checks the size of the array using the `x.size` attribute. Since `x` contains three elements, its size is not equal to zero. Therefore, the condition `x.size == 0` evaluates to False. As a result, the code enters the `else` block and prints “Array is not empty”.

12). What is the output of the following code?

				
					import numpy as np
x = np.array([3, 5, 1])
print("Product of the elements in the given array: ", x.prod())

				
			

a) 3
b) 15
c) 8
d) 11

Correct answer is: b) 15
Explanation: The given code imports the NumPy library and creates a 1-dimensional NumPy array `x` with the elements [3, 5, 1]. The `x.prod()` function is then used to calculate the product of all elements in the array `x`.

13). What is the purpose of the following code?

				
					import pandas as pd
import numpy as np
 
x = np.arange(1, 16).reshape(3, 5)
 
DF = pd.DataFrame(x)
DF.to_csv("data1.csv")

df = pd.read_csv("data1.csv")
print(df)
				
			

a) It generates a NumPy array with numbers from 1 to 15 and reshapes it to a 3×5 matrix, then saves it as a CSV file.
b) It reads a CSV file named “data1.csv” and prints its content as a Pandas DataFrame.
c) It generates a Pandas DataFrame from a CSV file named “data1.csv” and prints its content.
d) It converts a NumPy array to a Pandas DataFrame and saves it as a CSV file named “data1.csv”.

Correct answer is: a) It generates a NumPy array with numbers from 1 to 15 and reshapes it to a 3×5 matrix, then saves it as a CSV file.
Explanation: `import pandas as pd`: Imports the Pandas library with the alias `pd`. `import numpy as np`: Imports the NumPy library with the alias `np`. `x = np.arange(1, 16).reshape(3, 5)`: Generates a NumPy array with numbers from 1 to 15 using `np.arange()` and reshapes it to a 3×5 matrix using `reshape()` method. `DF = pd.DataFrame(x)`: Creates a Pandas DataFrame `DF` from the NumPy array `x`. `DF.to_csv(“data1.csv”)`: Saves the Pandas DataFrame `DF` as a CSV file named “data1.csv”. `df = pd.read_csv(“data1.csv”)`: Reads the CSV file “data1.csv” and creates a new Pandas DataFrame `df`. `print(df)`: Prints the content of the Pandas DataFrame `df`.

14). What is the purpose of the following code?

				
					import numpy as np
x = np.array([[6, 5, 7], [10, 9, 1], [7, 8, 2]])

for i in range(0, 2):
    print(f"{i+1} column: ", x[:, i])
				
			

a) It prints the sum of elements in each row of the array `x`.
b) It prints the transpose of the array `x`.
c) It prints the first two columns of the array `x`.
d) It prints the elements of the second row in reverse order.

Correct answer is: c) It prints the first two columns of the array `x`.
Explanation: The given code imports the NumPy library and creates a 3×3 2-dimensional NumPy array `x` with the elements. The code then uses a for loop to iterate over the columns of `x` with the range from 0 to 2 (excluding 2). For each iteration, it prints the respective column of the array `x` using the slicing syntax `x[:, i]`, where `i` is the column index.

15). What is the purpose of the following code?

				
					import numpy as np
x = np.array([[6, 5, 7], [10, 9, 1], [7, 8, 2]])
print(x[0:2, 0:2])
				
			

a) Prints the first two elements of each row.
b) Prints the first two rows and first two columns of the array.
c) Prints the first two rows and first three columns of the array.
d) Prints the first two elements of each column.

Correct answer is: b) Prints the first two rows and first two columns of the array.
Explanation: The given code imports the NumPy library and creates a 2-dimensional NumPy array `x` with the elements: The code then uses slicing to extract a sub-array from `x` using the notation `x[0:2, 0:2]`.
Slicing notation `x[0:2, 0:2]` means the following:
– `0:2` selects the first and second rows (index 0 and 1) of the array.
– `0:2` selects the first and second columns (index 0 and 1) of the array.

16). What is the output of the following code?

				
					import numpy as np
x = np.array([[6, 5, 7], [10, 9, 1], [7, 8, 2]])
print("Sum of all columns: ", x.sum(axis=0))

				
			

a) [23, 22, 10]
b) [6, 5, 7, 10, 9, 1, 7, 8, 2]
c) [17, 22, 10]
d) [23, 9, 17]

Correct answer is: a) [23, 22, 10]
Explanation: The given code imports the NumPy library and creates a 2-dimensional NumPy array `x` with the elements: The `x.sum(axis=0)` calculates the sum of all columns (along axis 0) in the array `x`.
The sum of each column is calculated as follows:
Column 1 sum: 6 + 10 + 7 = 23
Column 2 sum: 5 + 9 + 8 = 22
Column 3 sum: 7 + 1 + 2 = 10

17). What is the output of the following code?

				
					import numpy as np
x = np.array([[6, 5, np.nan], [np.nan, 9, 1], [7, 8, 2]])

temp = np.ma.masked_array(x, np.isnan(x))
print("Array after removing nan values: \n", temp)

				
			

a)
[[6.0 5.0 –]
[– 9.0 1.0]
[7.0 8.0 2.0]]

b)
[[6.0 5.0 0.0]
[0.0 9.0 1.0]
[7.0 8.0 2.0]]

c)
[[6.0 5.0 0.0]
[0.0 9.0 1.0]
[7.0 8.0 2.0]]

d)
[[6.0 5.0 1.0]
[1.0 9.0 1.0]
[7.0 8.0 2.0]]

Correct answer is: a)
[[6.0 5.0 –]
[– 9.0 1.0]
[7.0 8.0 2.0]]
Explanation: The given code creates a 2-dimensional NumPy array `x` with the elements [[6, 5, np.nan], [np.nan, 9, 1], [7, 8, 2]]. The `np.isnan()` function is used to identify the NaN (Not a Number) values in the array `x`. The `np.ma.masked_array()` function then creates a masked array where the NaN values are masked or hidden. The masked array `temp` will have the NaN values replaced with ‘–‘, representing that these values are masked and not considered in computations.

18). What is the output of the following code?

				
					import numpy as np
x = np.array([1, 2, 3])
y = np.array([4, 5, 6])

output = np.concatenate((x, y))
print("\nAfter concatenate:")
print(output)
				
			

a) [1, 2, 3, 4, 5, 6]
b) [[1, 2, 3], [4, 5, 6]]
c) [[1, 2, 3, 4, 5, 6]]
d) [5, 7, 9]

Correct answer is: a) [1, 2, 3, 4, 5, 6]
Explanation: The given code imports the NumPy library and creates two 1-dimensional NumPy arrays, `x` and `y`. The `np.concatenate()` function is then used to concatenate these two arrays. The `np.concatenate()` function concatenates arrays along a specified axis. If the axis is not specified, it defaults to 0, which means the arrays will be concatenated along the first dimension.

19). What is the purpose of the following code?

				
					import numpy as np
import pandas as pd
x = np.random.rand(6, 3)

df = pd.DataFrame(x, columns=['A', 'B', 'C'])
print("\nPandas DataFrame: ")
print(df)
				
			

a) It generates a NumPy array of random floating-point numbers with a shape of (6, 3).
b) It generates a Pandas DataFrame with 6 rows and 3 columns containing random floating-point numbers.
c) It calculates the mean of a Pandas DataFrame and prints the result.
d) It creates a Pandas DataFrame from a given NumPy array and prints the DataFrame.

Correct answer is: b) It generates a Pandas DataFrame with 6 rows and 3 columns containing random floating-point numbers.
Explanation:
1. The code imports the NumPy library as `np` and the Pandas library as `pd`.
2. The `np.random.rand(6, 3)` function generates a NumPy array of random floating-point numbers with a shape of (6, 3).
3. The `pd.DataFrame()` function takes the NumPy array `x` and converts it into a Pandas DataFrame with 6 rows and 3 columns. The columns are labeled as ‘A’, ‘B’, and ‘C’ respectively, as specified by the `columns` parameter.
4. The `print(df)` statement displays the Pandas DataFrame, showing the 6 rows and 3 columns of random floating-point numbers.

20). What is the output of the following code?

				
					import numpy as np
x = np.arange(15).reshape(3, 5)
y = np.arange(20).reshape(4, 5)

if x.shape == y.shape:
    print("Same dimensions")
else:
    print("Different dimensions")
				
			

a) Same dimensions
b) Different dimensions

Correct answer is: b) Different dimensions
Explanation: The given code imports the NumPy library and creates two NumPy arrays, `x` and `y`. The array `x` is created using `np.arange(15)` and reshaped into a 3×5 array, while the array `y` is created using `np.arange(20)` and reshaped into a 4×5 array. Next, the code checks whether the shapes of arrays `x` and `y` are equal using the `x.shape == y.shape` condition. If the shapes are equal, it prints “Same dimensions”, and if the shapes are different, it prints “Different dimensions”.

21). What is the output of the following code?

				
					import numpy as np
x = np.array([[6, 5, 7], [10, 9, 1], [7, 8, 2]])

print((~x.any(axis=0)).any())
				
			

a) True
b) False

Correct answer is: b) False
Explanation: The given code imports the NumPy library and creates a 2-dimensional NumPy array `x` with the elements [[6, 5, 7], [10, 9, 1], [7, 8, 2]]. The expression `(~x.any(axis=0))` is used to find the columns in the array `x` that contain at least one nonzero element. The `x.any(axis=0)` returns a boolean array indicating for each column whether it contains at least one nonzero element.

22). What is the output of the following code?

				
					import numpy as np
angle = np.array([45, 13])
print(f"Sin of {angle}: ", np.sin(angle))
				
			

a) [0.85090352, 0.42016704]
b) [0.52532199, 0.96355819]
c) [0.01745241, 0.85607777]
d) [0.57357644, 0.14112001]

Correct answer is: a) [0.85090352, 0.42016704]
Explanation: The given code imports the NumPy library and creates a NumPy array `angle` with the elements [45, 13]. The `np.sin()` function is then used to calculate the sine of the angles in the `angle` array. The sine of an angle in degrees can be calculated using the NumPy `sin()` function, which takes an array of angles in radians as input. To convert degrees to radians, we need to multiply the angle by π/180.

23). What is the purpose of the following code?

				
					import numpy as np
age = np.array([20, 25, 21, 23, 26, 27, 26, 30, 24, 26])

print("Standard Deviation: ", np.std(age))
				
			

a) It calculates the mean age of the given data.
b) It calculates the variance of the ages in the dataset.
c) It calculates the standard deviation of the ages in the dataset.
d) It calculates the total number of elements in the array.

Correct answer is: c) It calculates the standard deviation of the ages in the dataset.
Explanation: The given code imports the NumPy library and creates a NumPy array `age` containing the ages of a group of individuals. The `np.std()` function is then used to calculate the standard deviation of the ages in the dataset. The standard deviation is a measure of how spread out the values are from the mean. It is calculated using the formula:
Standard Deviation = sqrt((Σ (x – mean)^2) / N)
where:
– `x` represents each individual value (age) in the dataset.
– `mean` is the mean (average) of the ages.
– `N` is the total number of elements (ages) in the dataset.

24). What is the purpose of the following code?

				
					import numpy as np
arr = np.array([12.202, 90.23120, 123.88, 23.202])  
print(np.round(arr, 2))
				
			

a) It calculates the mean of the array elements.
b) It calculates the standard deviation of the array elements.
c) It rounds the array elements to the nearest integer.
d) It rounds the array elements to two decimal places.

Correct answer is: d) It rounds the array elements to two decimal places.
Explanation: The given code imports the NumPy library and creates a NumPy array `arr` with the elements [12.202, 90.23120, 123.88, 23.202]. The `np.round()` function is then used to round the elements of the array `arr` to two decimal places. The rounded array elements are as follows:
Rounded array = [12.20, 90.23, 123.88, 23.20]

25). What is the output of the following code?

				
					import numpy as np
arr = np.array([12.202, 90.23120, 123.88, 23.202])

print(np.floor(arr))
				
			

a) [12, 90, 123, 23]
b) [12.0, 90.0, 123.0, 23.0]
c) [13, 91, 124, 24]
d) [12.202, 90.231, 123.88, 23.202]

Correct answer is: a) [12, 90, 123, 23]
Explanation: The given code imports the NumPy library and creates a 1-dimensional NumPy array `arr` with the elements [12.202, 90.23120, 123.88, 23.202]. The `np.floor()` function is then used to round down each element in the array `arr` to the nearest integer.

NumPy MCQ : Set 5

Python NumPy MCQ

1). What is the output of the following code?

				
					import numpy as np
x = np.array([[5, 3],
              [7, 4]])
print("Sum of each row : ", np.sum(x, axis=1))

				
			

a) [8, 11]
b) [12, 17]
c) [6, 11]
d) [9, 14]

Correct answer is: a) [8, 11]
Explanation: The given code imports the NumPy library and creates a 2-dimensional NumPy array `x` with the elements [[5, 3], [7, 4]]. The `np.sum()` function is then used to calculate the sum of each row in the array `x` along `axis=1`.

2). What is the output of the following code?

				
					import numpy as np
x = np.array([[5, 3],
              [7, 4]])
print("Sum of each column: ", np.sum(x, axis=0))

				
			

a) [12, 7]
b) [8, 9]
c) [19, 7]
d) [5, 3, 7, 4]

Correct answer is: a) [12, 7]
Explanation: The given code imports the NumPy library and creates a 2-dimensional NumPy array `x` with the elements [[5, 3], [7, 4]]. The `np.sum()` function is then used to calculate the sum of each column in the array `x` along `axis=0`.

3). What is the output of the following code?

				
					import numpy as np
x = np.array([5, 3])
y = np.array([7, 4])
print("Inner-dot product: ", np.dot(x, y))
				
			

a) 31
b) 29
c) 39
d) 33

Correct answer is: a) 47
Explanation: The given code imports the NumPy library and creates two 1-dimensional NumPy arrays `x` and `y` with the elements [5, 3] and [7, 4] respectively. The `np.dot()` function is then used to calculate the inner-dot product of the arrays `x` and `y`.

4). What is the output of the following code?

				
					import numpy as np
x = np.array([[5, 3],
              [7, 4]])
print("Original matrix: \n", x)
vector = np.array([2, 5])
print("Vector to be added: \n", vector)

for i in range(len(x)):
    x[i, :] = x[i, :] + vector
    
print("Matrix after adding a vector: \n", x)
				
			

a)
Original matrix:
[[5 3]
[7 4]]
Vector to be added:
[2 5]
Matrix after adding a vector:
[[7 8]
[9 9]]

b)
Original matrix:
[[5 3]
[7 4]]
Vector to be added:
[2 5]
Matrix after adding a vector:
[[ 7 8]
[ 9 10]]

c)
Original matrix:
[[5 3]
[7 4]]
Vector to be added:
[2 5]
Matrix after adding a vector:
[[ 7 5]
[ 9 4]]

d)
Original matrix:
[[5 3]
[7 4]]
Vector to be added:
[2 5]
Matrix after adding a vector:
[[ 7 5]
[ 9 9]]

Correct answer is: a)
Original matrix:
[[5 3]
[7 4]]
Vector to be added:
[2 5]
Matrix after adding a vector:
[[7 8]
[9 9]]
Explanation: The given code initializes a 2-dimensional NumPy array `x` with the elements [[5, 3], [7, 4]] and a 1-dimensional NumPy array `vector` with the elements [2, 5]. The code then adds the `vector` to each row of the `x` array in a loop.

5). What is the purpose of the following code?

				
					import numpy as np
x = np.array([[3, 0],
             [6, 4]])
print("Matrix: \n", x)
y = x.tolist()
print("List: ", y)
				
			

a) The code calculates the sum of all elements in the NumPy array `x`.
b) The code converts the NumPy array `x` into a list `y`.
c) The code reshapes the NumPy array `x` into a 1-dimensional array.
d) The code transposes the rows and columns of the NumPy array `x`.

Correct answer is: b) The code converts the NumPy array `x` into a list `y`.
Explanation: The given code snippet demonstrates the use of NumPy and Python’s built-in `tolist()` function. 

6). What is the output of the following code?

				
					import numpy as np
x = np.array([[1, 5, np.nan],
              [8, np.nan, 9]])
print(np.isnan(x))
				
			

a) [[False, False, True], [False, True, False]]
b) [[True, True, False], [False, False, True]]
c) [[False, True, False], [True, False, False]]
d) [[False, False, False], [False, False, False]]

Correct answer is: a) [[False, False, True], [False, True, False]]
Explanation: The given code imports the NumPy library and creates a 2-dimensional NumPy array `x` with some elements being NaN (Not a Number). The `np.isnan()` function is then used to check if the elements in the array are NaN or not. The output of `np.isnan(x)` will be a Boolean array with the same shape as `x`, where each element is `True` if the corresponding element in `x` is NaN, and `False` otherwise.

7). What is the output of the following code?

				
					import numpy as np
x = np.array([[5, 6],
              [7, 8],
              [9, 10]])
print(x.transpose())
				
			

a) [[5, 6],
[7, 8],
[9, 10]]

b) [[5, 7, 9],
[6, 8, 10]]

c) [[5, 7, 9],
[6, 8, 10],
[9, 10]]

d) [[5, 6],
[6, 7],
[7, 8],
[8, 9],
[9, 10]]

Correct answer is: b) [[5, 7, 9],
[6, 8, 10]]
Explanation: The given code imports the NumPy library and creates a 2-dimensional NumPy array `x` with the elements [[5, 6], [7, 8], [9, 10]]. The `x.transpose()` function is then used to calculate the transpose of the array `x`. The transpose of a 2-dimensional array is obtained by flipping the rows and columns. So, the resulting array will have the elements of `x` interchanged between rows and columns.

8). What is the output of the following code?

				
					import numpy as np
x = np.array([[6, 5],
              [10, 9],
              [8, 7]])

print("Sorting matrix by row: ")
print(np.sort(x))
				
			

a) [[5 6]
[9 10]
[7 8]]
b) [[6 5]
[10 9]
[8 7]]
c) [[5 6]
[7 8]
[9 10]]
d) [[10 9]
[8 7]
[6 5]]

Correct answer is: c) [[5 6]
[7 8]
[9 10]]
Explanation: The given code imports the NumPy library and creates a 2-dimensional NumPy array `x` with the elements [[6, 5], [10, 9], [8, 7]]. The `np.sort()` function is then used to sort the elements of `x` along each row.

9). What is the output of the following code?

				
					import numpy as np
x = np.array([[6, 5],
              [10, 9],
              [8, 7]])

print("Sorting matrix by column: ")
print(np.sort(x, axis=0))
				
			

a)
[[ 5 5]
[ 8 7]
[10 9]]

b)
[[ 5 6]
[ 7 8]
[ 9 10]]

c)
[[ 6 5]
[ 9 10]
[ 7 8]]

d)
[[ 7 5]
[ 8 6]
[10 9]]

Correct answer is: b)
[[ 5 6]
[ 7 8]
[ 9 10]]
Explanation: The given code imports the NumPy library and creates a 2-dimensional NumPy array `x` with the elements [[6, 5], [10, 9], [8, 7]]. The `np.sort()` function is then used to sort the elements of the array `x` along axis 0, which means sorting along the columns.

10). What is the output of the following code?

				
					import numpy as np
x = np.array([[6, 5],
              [10, 9],
              [8, 7]])

print("Values bigger than 7 =", x[x>7])
				
			

a) Values bigger than 7 = [10, 9, 8, 7]
b) Values bigger than 7 = [10, 9, 8]
c) Values bigger than 7 = [8, 7]
d) Values bigger than 7 = [6, 5]

Correct answer is: b) Values bigger than 7 = [10, 9, 8]
Explanation: The given code imports the NumPy library and creates a 2-dimensional NumPy array `x` with the elements [[6, 5], [10, 9], [8, 7]]. The code then uses boolean indexing (`x>7`) to extract the elements from `x` that are bigger than 7.

11). What is the output of the following code?

				
					import numpy as np
x = np.array([[6, 5],
              [10, 9],
              [8, 7]])

x[x >= 7] = 0
print("New matrix")
print(x)
				
			

a) [[0, 0], [0, 0], [0, 0]]
b) [[6, 5], [10, 9], [8, 7]]
c) [[6, 5], [0, 0], [0, 0]]
d) [[0, 5], [0, 9], [0, 7]]

Correct answer is: c) [[6, 5], [0, 0], [0, 0]]
Explanation: The given code imports the NumPy library and creates a 2-dimensional NumPy array `x` with the elements [[6, 5], [10, 9], [8, 7]]. It then modifies the elements of the array `x` based on the condition `x >= 7`. Any element in the array that is greater than or equal to 7 is replaced with 0.

12). What is the purpose of the following code?

				
					import numpy as np
x = np.array([[6, 5],
              [10, 9],
              [8, 7]])
print("Original array: ", x)
print("After swapping")
new = print(x[::-1, :])
				
			

a) The code transposes the original array.
b) The code reverses the rows of the original array.
c) The code reverses the columns of the original array.
d) The code prints the original array in reverse order.

Correct answer is: b) The code reverses the rows of the original array.
Explanation: The given code imports the NumPy library and creates a 2-dimensional NumPy array `x` with the elements [[6, 5], [10, 9], [8, 7]]. It then prints the original array using `print(“Original array: “, x)`. The line `new = print(x[::-1, :])` involves array slicing. Specifically, `x[::-1, :]` reverses the rows of the original array `x`.

13). What is the output of the following code?

				
					import numpy as np
x = np.array([[6, 5],
              [10, 9],
              [8, 7]])

x[0,:] = x[0,:]*2
print("After multiplying the first row by 2: ", x)
				
			

a) [[12, 10], [10, 9], [8, 7]]
b) [[12, 10], [20, 18], [16, 14]]
c) [[6, 5], [10, 9], [8, 7]]
d) [[6, 5], [10, 9], [8, 7, 2]]

Correct answer is: a) [[12, 10], [10, 9], [8, 7]]
Explanation: The given code imports the NumPy library and creates a 2-dimensional NumPy array `x` with the elements [[6, 5], [10, 9], [8, 7]]. Next, it modifies the first row of the array `x` by multiplying it by 2.

14). What is the purpose of the following code?

				
					import numpy as np
x = np.array([[6, 5],
              [10, 9],
              [8, 7]])

x[0,:] = x[0,:] + x[1,:]
print("After addition: ", x)
				
			

a) It transposes the matrix `x`
b) It calculates the element-wise addition of the first and second rows in the matrix `x`
c) It calculates the dot product of the first and second rows in the matrix `x`
d) It reshapes the matrix `x` into a 1-dimensional array

Correct answer is: b) It calculates the element-wise addition of the first and second rows in the matrix `x`
Explanation: The given code snippet imports the NumPy library and creates a 2-dimensional NumPy array `x` with the elements [[6, 5], [10, 9], [8, 7]]. The line `x[0,:] = x[0,:] + x[1,:]` performs element-wise addition of the first row (`[6, 5]`) and the second row (`[10, 9]`) of the matrix `x`. The result is then assigned back to the first row of the matrix `x`.

15). What is the output of the following code?

				
					import numpy as np
x = np.array([6+2j, 10+5j])
print("Real part: ", x.real)
print("Imaginary part: ", x.imag)
				
			

a) Real part: [6, 10], Imaginary part: [2, 5]
b) Real part: [8, 15], Imaginary part: [2, 5]
c) Real part: [6, 10], Imaginary part: [2j, 5j]
d) Real part: [8, 15], Imaginary part: [2j, 5j]

Correct answer is: a) Real part: [6, 10], Imaginary part: [2, 5]
Explanation: The given code imports the NumPy library and creates a 1-dimensional NumPy array `x` with complex numbers: [6+2j, 10+5j]. The `real` attribute of the complex array `x` gives the real parts of the complex numbers, and the `imag` attribute gives the imaginary parts.

16). What is the output of the following code?

				
					import numpy as np
x = np.array([56, 18, 28, 36])
y = np.array([76, 36, 56])
print(np.intersect1d(x, y))

				
			

a) [56, 18, 28, 36]
b) [56, 36]
c) [18, 28, 76]
d) [76, 18, 28]

Correct answer is: b) [56, 36]
Explanation: The given code imports the NumPy library and creates two 1-dimensional NumPy arrays, `x` and `y`, with the elements [56, 18, 28, 36] and [76, 36, 56] respectively. The `np.intersect1d()` function is then used to find the intersection of the elements between the arrays `x` and `y`.

17). What is the output of the following code?

				
					import numpy as np
x = np.array([25, 33, 10, 45, 33, 10])
print(np.unique(x))
				
			

a) [10, 25, 33, 45]
b) [10, 33, 45]
c) [25, 33, 10, 45]
d) [25, 33, 10]

Correct answer is: a) [10, 25, 33, 45]
Explanation: The given code imports the NumPy library and creates a 1-dimensional NumPy array `x` with the elements [25, 33, 10, 45, 33, 10]. The `np.unique()` function is then used to find the unique elements in the array `x`.

18). What is the output of the following code?

				
					import numpy as np
x = np.array([56, 18, 28, 36])
y = np.array([76, 36, 56])

print(np.setdiff1d(x, y))
				
			

a) [56, 18, 28, 36]
b) [18, 28]
c) [76]
d) [18, 28, 76]

Correct answer is: b) [18, 28]
Explanation: The given code imports the NumPy library and creates two 1-dimensional NumPy arrays, `x` and `y`, with the elements [56, 18, 28, 36] and [76, 36, 56], respectively. The `np.setdiff1d()` function is then used to find the set difference between the arrays `x` and `y`. The set difference between two arrays contains the elements that are present in the first array but not in the second array.

19). What is the output of the following code?

				
					import numpy as np
x = np.array([56, 18, 28, 36])
y = np.array([76, 36, 56, 90])

print(np.union1d(x, y))
				
			

a) [18, 28, 36, 56, 76, 90]
b) [56, 18, 28, 36, 76, 90]
c) [56, 18, 28, 36]
d) [18, 28, 36, 76, 90]

Correct answer is: a) [18, 28, 36, 56, 76, 90]
Explanation: The given code imports the NumPy library and creates two 1-dimensional NumPy arrays, `x` and `y`, with the elements [56, 18, 28, 36] and [76, 36, 56, 90] respectively. The `np.union1d()` function is then used to find the unique values that are in either of the arrays. The union of `x` and `y` will contain all the unique elements present in both arrays, without any duplicates. The output will be a new sorted 1-dimensional array with these unique elements.

20). What is the output of the following code?

				
					import numpy as np
x = np.array([76, 36, 56, 90])
print("Index of maximum Values: ", np.argmax(x))
				
			

a) 0
b) 1
c) 2
d) 3

Correct answer is: d) 3
Explanation: The given code imports the NumPy library and creates a 1-dimensional NumPy array `x` with the elements [76, 36, 56, 90]. The `np.argmax()` function is then used to find the index of the maximum value in the array `x`.

21). What is the output of the following code?

				
					import numpy as np
x = np.array([76, 36, 56, 90])
print("Index of maximum Values: ", np.argmin(x))
				
			

a) 1
b) 2
c) 3
d) 4

Correct answer is: b) 2
Explanation: The given code imports the NumPy library and creates a 1-dimensional NumPy array `x` with the elements [76, 36, 56, 90]. The `np.argmin()` function is then used to find the index of the minimum value in the array `x`.

22). What is the output of the following code?

				
					import numpy as np
x = np.array([[4, 8, 7], [2, 3, 6]])
print("Fifth element: ", x[1, 1])
				
			

a) 4
b) 8
c) 3
d) 6

Correct answer is: c) 3
Explanation: The given code imports the NumPy library and creates a 2-dimensional NumPy array `x` with the elements [[4, 8, 7], [2, 3, 6]]. In NumPy arrays, indexing starts from 0. The expression `x[1, 1]` accesses the element at the second row and second column of the array `x`.

23). What is the output of the following code?

				
					
import numpy as np
x = np.zeros((2, 1, 3))
print("After removing single-dimensional entries: ", np.squeeze(x).shape)
				
			

a) (2, 1, 3)
b) (2, 3)
c) (1, 3)
d) (3,)

Correct answer is: b) (2, 3)
Explanation: The given code imports the NumPy library and creates a 3-dimensional NumPy array `x` with the shape (2, 1, 3) filled with zeros. The `np.squeeze()` function is then used to remove single-dimensional entries from the array `x`. The `np.squeeze()` function removes any dimensions from the array that have a size of 1. In this case, the second dimension has a size of 1, so it is removed from the array. Before squeezing, the shape of `x` is (2, 1, 3), which means it has 2 elements along the first dimension, 1 element along the second dimension, and 3 elements along the third dimension. After squeezing, the shape becomes (2, 3) because the second dimension with a size of 1 is removed. Therefore, the correct answer is (2, 3).

24). What is the output of the following code?

				
					import numpy as np
x = np.array((3, 8, 5))
y = np.array((4, 0, 7))
z = np.column_stack((x, y))
print(z)
				
			

a) [[3, 4], [8, 0], [5, 7]]
b) [[3, 8, 5], [4, 0, 7]]
c) [[3, 8], [4, 0], [5, 7]]
d) [[4, 3], [0, 8], [7, 5]]

Correct answer is: a) [[3, 4], [8, 0], [5, 7]]
Explanation: The given code imports the NumPy library and creates two 1-dimensional NumPy arrays `x` and `y` with the elements (3, 8, 5) and (4, 0, 7) respectively. Then, the `np.column_stack()` function is used to stack the arrays `x` and `y` column-wise, forming a new 2-dimensional array `z`.

25). What is the output of the following code?

				
					import numpy as np
x = np.arange(1, 11)
print("Original array:", x)
print("After splitting:")
print(np.split(x, [2, 5]))
				
			

a) Original array: [1 2 3 4 5 6 7 8 9 10]
After splitting: [array([1, 2]), array([3, 4, 5]), array([ 6, 7, 8, 9, 10])]

b) Original array: [1 2 3 4 5 6 7 8 9 10]
After splitting: [array([1, 2]), array([3, 4, 5, 6]), array([7, 8, 9, 10])]

c) Original array: [1 2 3 4 5 6 7 8 9 10]
After splitting: [array([1, 2]), array([3, 4, 5]), array([6, 7, 8, 9, 10])]

d) Original array: [1 2 3 4 5 6 7 8 9 10]
After splitting: [array([1, 2]), array([3, 4]), array([5, 6, 7, 8, 9, 10])]

Correct answer is: a) Original array: [1 2 3 4 5 6 7 8 9 10]
After splitting: [array([1, 2]), array([3, 4, 5]), array([ 6, 7, 8, 9, 10])]
Explanation: The given code imports the NumPy library and creates a 1-dimensional NumPy array `x` with the elements [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]. The `np.split()` function is then used to split the array into subarrays. The `np.split(x, [2, 5])` function call splits the array `x` at the indices [2, 5]. It means the array will be split into three subarrays: from index 0 to index 2 (exclusive), from index 2 to index 5 (exclusive), and from index 5 to the end of the array.

NumPy MCQ : Set 4

NumPy MCQ

1). What is the output of the following code?

				
					import numpy as np
x = np.array([6, 8, 3, 5])
print(np.all(x))
				
			

a) True
b) False
c) 0
d) [True, True, True, True]

Correct answer is: b) False
Explanation: The code snippet imports the NumPy library and creates an array `x` with the values [6, 8, 3, 5]. The `np.all()` function is then used to check if all elements in the array are true. However, in NumPy, the value `0` is considered as False, and any non-zero value is considered as True. In this case, the array `x` contains non-zero values (6, 8, 3, 5), so `np.all(x)` will return `True` if all elements in the array are non-zero. Since the array contains zero (`3`), the function will return `False`.

2). What is the output of the following code?

				
					import numpy as np
x = np.array([6, 8, 3, np.inf])
print(np.isfinite(x))
				
			

a) [ True True True False]
b) [ True False True False]
c) [ True True True True]
d) [False False False False]

Correct answer is: a) [True True True False]
Explanation: The given code creates a NumPy array `x` containing four elements: `[6, 8, 3, np.inf]`. The `np.isfinite()` function is applied to the array, which returns a new Boolean array with the same shape as the input array. In the output, `True` represents finite numbers, and `False` represents non-finite numbers (such as `np.inf`).

3). What is the output of the following code?

				
					import numpy as np
x = np.array([2, 4, np.nan, 7])
print(np.isnan(x))
				
			

a) [False, False, False, False]
b) [True, True, True, True]
c) [False, False, True, False]
d) [True, False, True, False]

Correct answer is: c) [False, False, True, False]
Explanation: The given code imports the NumPy library as np and creates a NumPy array ‘x’ with the values [2, 4, np.nan, 7]. The ‘np.isnan(x)’ function checks for NaN (Not a Number) values in the array ‘x’.

4). What is the output of the following code?

				
					import numpy as np
x = np.array([2, 4, 6+9j, 7])
print(np.iscomplex(x))
				
			

a) [False, False, True, False]
b) [True, True, True, True]
c) [False, False, False, False]
d) [True, False, True, False]

Correct answer is: a) [False, False, True, False]
Explanation: The code snippet imports the NumPy library as np and creates an array ‘x’ containing four elements: 2, 4, 6+9j, and 7. The ‘np.iscomplex()’ function is then used to check whether each element in the array ‘x’ is complex or not. The ‘np.iscomplex()’ function returns a Boolean array with the same shape as the input array, where each element is ‘True’ if the corresponding element in the input array is complex, and ‘False’ otherwise.

5). What is the output of the following code?

				
					import numpy as np
x = np.array([2, 4, 6 + 9j, 7])
print(np.isreal(x))
				
			

a) [ True True False True]
b) [ True True True True]
c) [ True True True False]
d) [ True True False False]

Correct answer is: a) [ True True False True]
Explanation: The code creates a NumPy array `x` with four elements: 2, 4, 6+9j (complex number), and 7. The `np.isreal()` function is then applied to this array. The `np.isreal()` function checks if each element in the array is a real number (not a complex number). It returns a Boolean array, where `True` indicates that the element is a real number, and `False` indicates that the element is a complex number.

6). What is the output of the following code?

				
					import numpy as np
x = 8.9
print(np.isscalar(x))

y = [8.9]
print(np.isscalar(y))
				
			

a) True, True
b) False, False
c) True, False
d) False, True

Correct answer is: c) True, False
Explanation: The `np.isscalar(x)` function returns True if the input `x` is a scalar (a single numeric value) and False if it is an array or any other type. In the first case, `x` is assigned the value 8.9, which is a scalar (float), so the output of `np.isscalar(x)` is True. In the second case, `y` is assigned the value `[8.9]`, which is a list containing one element. Lists are not considered scalars in NumPy. Therefore, the output of `np.isscalar(y)` is False.

7). What is the output of the following code?

				
					import numpy as np
x = np.array([3, 8, 5])
y = np.array([3, 8, 6])
print(x == y)
				
			

a) [True, True, True]
b) [False, False, False]
c) [True, True, False]
d) [False, True, True]

Correct answer is: c) [True, True, False]
Explanation: The code snippet creates two NumPy arrays, `x` and `y`, and then performs an element-wise comparison using the `==` operator. The resulting array will have the same shape as the input arrays, and each element in the output will be `True` if the corresponding elements in `x` and `y` are equal and `False` otherwise.

8). What is the output of the following code?

				
					import numpy as np
x = np.array([8, 3])
y = np.array([9, 1])
print(x > y)
				
			

a) [False, False]
b) [True, True]
c) [False, True]
d) [True, False]

Correct answer is: c) [False, True]
Explanation: The code snippet creates two NumPy arrays `x` and `y`, each with two elements. It then applies the comparison `x > y`, which checks element-wise whether the corresponding elements of `x` are greater than the corresponding elements of `y`.

9). What is the output of the following code?

				
					import numpy as np
x = np.array([6, 4])
y = np.array([6, 3])
print(x >= y)
				
			

a) [True, False]
b) [False, True]
c) [True, True]
d) [False, False]

Correct answer is: c) [True, True]
Explanation: The code snippet imports the NumPy library, creates two NumPy arrays ‘x’ and ‘y’, and then prints the result of the comparison ‘x >= y’. The comparison operator ‘>=’ checks if each element of ‘x’ is greater than or equal to the corresponding element in ‘y’. The result of the comparison is a Boolean array that shows the comparison result for each element.

10). What is the output of the following code?

				
					import numpy as np
x = np.array([6, 4])
y = np.array([2, 9])
print(x < y)
				
			

a) [False, True]
b) [True, False]
c) [True, True]
d) [False, False]

Correct answer is: a) [False, True]
Explanation: The given code snippet imports the NumPy library as np and creates two NumPy arrays, x and y. The arrays are [6, 4] and [2, 9], respectively. The statement `x < y` performs element-wise comparison between the elements of arrays x and y.

11). What is the output of the following code?

				
					import numpy as np
x = np.array([10, 34, 86, 26, 56])

print("Size of the array: ", x.size)
print("Memory size of one array element in bytes: ", x.itemsize)
print("Memory size of numpy array in bytes:", x.size * x.itemsize)
				
			

a) Size of the array: 5
Memory size of one array element in bytes: 4
Memory size of numpy array in bytes: 20

b) Size of the array: 5
Memory size of one array element in bytes: 8
Memory size of numpy array in bytes: 40

c) Size of the array: 5
Memory size of one array element in bytes: 16
Memory size of numpy array in bytes: 80

d) Size of the array: 5
Memory size of one array element in bytes: 32
Memory size of numpy array in bytes: 160

Correct answer is: a) Size of the array: 5
Memory size of one array element in bytes: 4
Memory size of numpy array in bytes: 20
Explanation: The given code snippet uses NumPy to create an array `x` with elements [10, 34, 86, 26, 56]. The three print statements then display information about the array.
1. Size of the array: The `x.size` attribute returns the number of elements in the array `x`, which is 5 in this case.
2. Memory size of one array element in bytes: The `x.itemsize` attribute returns the size of one array element in bytes. Since the array contains integer values, the size of each element is 4 bytes in most standard Python installations.
3. Memory size of numpy array in bytes: The memory size of the entire NumPy array can be calculated by multiplying the size of one array element (`x.itemsize`) with the number of elements in the array (`x.size`). In this case, the memory size of the numpy array is 20 bytes (5 elements * 4 bytes per element).

12). What is the output of the following code?

				
					import numpy as np
x = np.ones(3, dtype=int)
print(x)
				
			

a) [1, 1, 1]
b) [1, 1, 1, 1]
c) [1.0, 1.0, 1.0]
d) [1, 1, 1.0]

Correct answer is: a) [1, 1, 1]
Explanation: The code snippet imports the NumPy library as np and creates a NumPy array called `x` using the `np.ones()` function. The `np.ones()` function creates an array of ones with the specified shape. In this case, it creates an array with 3 elements, all set to 1, and the data type is specified as `int`.

13). What is the output of the following code?

				
					import numpy as np
x = np.arange(1, 11)
print("Array: ", x)
				
			

a) Array: [1 2 3 4 5 6 7 8 9 10]
b) [1 2 3 4 5 6 7 8 9 10]
c) [ 2 3 4 5 6 7 8 9 10]
d) Array: [ 1 2 3 4 5 6 7 8 9]

Correct answer is: a) Array: [1 2 3 4 5 6 7 8 9 10]]
Explanation: The code snippet imports the NumPy library as np and then uses np.arange(1, 11) to create a NumPy array `x` with elements starting from 1 up to, but not including, 11. The resulting array `x` is [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]. When printing the array using `print(“Array: “, x)`, it will display the array elements without the “Array:” label and in a 1-dimensional format.

14). What is the output of the following code?

				
					import numpy as np
x = np.arange(2, 11, 2)
print("Array of even numbers: ", x)
				
			

a) Array of even numbers: [2, 4, 6, 8, 10]
b) Array of even numbers: [2, 3, 4, 5, 6, 7, 8, 9, 10]
c) Array of even numbers: [1, 2, 3, 4, 5]
d) Array of even numbers: [2, 4, 6, 8]

Correct answer is: a) Array of even numbers: [2, 4, 6, 8, 10]
Explanation: In the code snippet, the NumPy function `np.arange()` is used to generate an array of even numbers starting from 2, incrementing by 2, and ending before 11 (exclusive). The arguments for `np.arange()` are `start`, `stop`, and `step`.

15). What is the output of the following code?

				
					import numpy as np
x = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(x)
				
			

a) [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
b) [1, 2, 3, 4, 5, 6, 7, 8, 9]
c) [[1 2 3]
[4 5 6]
[7 8 9]]
d) [[1 4 7]
[2 5 8]
[3 6 9]]

Correct answer is: c) [[1 2 3]
[4 5 6]
[7 8 9]]
Explanation: The code snippet imports the NumPy library as np and creates a 2-dimensional array called x using np.array(). The array contains three rows and three columns with the values:
[[1 2 3]
[4 5 6]
[7 8 9]]

16). What is the output of the following code?

				
					import numpy as np
x = np.identity(3)
print(x)
				
			

a) [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
b) [[0, 1, 0], [1, 0, 1], [0, 1, 0]]
c) [[1, 1, 1], [1, 1, 1], [1, 1, 1]]
d) [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

Correct answer is: a) [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
Explanation: The given code imports the NumPy library and then creates a 3×3 identity matrix using the `np.identity(3)` function. The identity matrix is a square matrix with ones on the main diagonal and zeros elsewhere. For a 3×3 identity matrix, it looks like this:
[[1, 0, 0],
[0, 1, 0],
[0, 0, 1]]

17). What is the purpose of the following code?

				
					from numpy import random
for i in range(5):
    rand_num = random.rand()
    print(rand_num)
				
			

a) To generate an array of 5 random integers between 0 and 1.
b) To generate a random float number between 0 and 1 and print it 5 times.
c) To generate an array of 5 random float numbers between 0 and 1.
d) To generate a random integer between 0 and 1 and print it 5 times.

Correct answer is: c) To generate an array of 5 random float numbers between 0 and 1.
Explanation: The code imports the `random` module from the NumPy library, which provides various functions for generating random numbers. The `random.rand()` function is used to generate random float numbers in the half-open interval [0, 1), meaning that 0 is included, but 1 is excluded. The code then enters a loop that iterates 5 times. In each iteration, the `random.rand()` function generates a new random float number and assigns it to the variable `rand_num`. The `print()` function is used to display this random float number. As a result, the code prints 5 random float numbers between 0 and 1, each on a separate line.

18). What is the purpose of the following code?

				
					import numpy as np
x = np.random.normal(size=5)
print(x)
				
			

a) It generates a random array of size 5 with normally distributed values.
b) It calculates the mean of a 5-element array using the normal distribution.
c) It generates an array of 5 random integers from a normal distribution.
d) It calculates the standard deviation of a 5-element array using the normal distribution.

Correct answer is: a) It generates a random array of size 5 with normally distributed values.
Explanation: The given code utilizes NumPy, a Python library for numerical computations, to achieve its purpose. The `np.random.normal()` function generates a random array of size 5 with values drawn from a normal distribution. The `size` parameter specifies the size of the array to be generated. In the context of this code, the variable `x` is assigned the randomly generated array of size 5. Each element in `x` is a random number sampled from a normal distribution with mean 0 and standard deviation 1, as that is the default behavior of `np.random.normal()`.

19). What is the output of the following code?

				
					import numpy as np
x = np.arange(1, 11)
for val in x:
    if val != 5:
        print(val, end=" ")
				
			

a) 1 2 3 4 5 6 7 8 9 10
b) 1 2 3 4 6 7 8 9 10
c) 1 2 3 4 6 7 8 9
d) 1 2 3 4 6 7 8 9 10 11

Correct answer is: b) 1 2 3 4 6 7 8 9 10
Explanation: The code snippet uses NumPy’s arange() function to create an array `x` with elements from 1 to 10 (inclusive). It then iterates over each element in the array using a for loop. Inside the loop, the condition `if val != 5:` checks if the current value is not equal to 5. If the condition is true, the value is printed using `print(val, end=” “)`.

20). What is the output of the following code?

				
					import numpy as np
x = np.arange(1, 13)
x.reshape(3, 4)
				
			

a) [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
b) [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
c) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
d) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

Correct answer is: a) [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
Explanation: The code snippet imports the NumPy library and creates an array ‘x’ using np.arange(1, 13), which generates an array of integers from 1 to 12 (inclusive of 1, exclusive of 13). The next line, x.reshape(3, 4), reshapes the ‘x’ array into a 2-dimensional array with 3 rows and 4 columns. The elements of the original ‘x’ array are distributed in row-major order to fill the new 3×4 array.

21). What is the output of the following code?

				
					import numpy as np
x = np.arange(1, 16)
x[(x >= 6) & (x <= 10)] *= -1
print(x)
				
			

a) [ 1 2 3 4 5 -6 -7 -8 -9-10 11 12 13 14 15]
b) [ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]
c) [ 1 2 3 4 5 6 -7 -8 -9-10 11 12 13 14 15]
d) [ 1 2 3 4 5 -6 7 8 9 10 11 12 13 14 15]

Correct answer is: a) [ 1 2 3 4 5 -6 -7 -8 -9-10 11 12 13 14 15]
Explanation: Let’s break down the code step by step: `import numpy as np`: Imports the NumPy library and assigns it the alias ‘np’. `x = np.arange(1, 16)`: Creates a NumPy array ‘x’ containing integers from 1 to 15 (inclusive), excluding 16. The array ‘x’ looks like this: [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]. `x[(x >= 6) & (x <= 10)] *= -1`: This line uses Boolean indexing to select the elements of ‘x’ that satisfy the condition (x >= 6) & (x <= 10), i.e., elements greater than or equal to 6 and less than or equal to 10. The selected elements are [6, 7, 8, 9, 10]. Then, it multiplies these selected elements by -1, effectively negating them. `print(x)`: Prints the modified ‘x’ array.

22). What is the output of the following code?

				
					import numpy as np
x = np.array([4, 2, 8])
y = np.array([7, 3, 5])
print(x * y)
				
			

a) [28, 6, 40]
b) [11, 5, 13]
c) [28, 6, 40, 11, 5, 13]
d) [ 28, 6, 40, 7, 3, 5]

Correct answer is: a) [28, 6, 40]
Explanation: The code snippet imports the NumPy library, creates two NumPy arrays x and y, and performs element-wise multiplication between the arrays using the * operator. When two NumPy arrays of the same shape are multiplied element-wise, each element of one array is multiplied by the corresponding element of the other array.

23). What is the output of the following code?

				
					import numpy as np
x = np.array([[5, 6, 7],
              [8, 9, 10]])
print("Shape of the matrix: ", x.shape)
				
			

a) Shape of the matrix: (3, 2)
b) Shape of the matrix: (2, 2)
c) Shape of the matrix: (2, 3)
d) Shape of the matrix: (3, 3)

Correct answer is: b) Shape of the matrix: (2, 3)
Explanation: The code defines a NumPy array `x` with two rows and three columns. The `x.shape` attribute returns a tuple representing the dimensions of the array, where the first element is the number of rows and the second element is the number of columns. In this case, the output will be “Shape of the matrix: (2, 3)” since `x` has 2 rows and 3 columns. The correct answer is option b).

24). What is the output of the following code?

				
					import numpy as np
x = np.diag([7, 8])
print(x)
				
			

a) [[7, 0], [0, 8]]
b) [7, 8]
c) [7, 8, 0, 0]
d) [[7], [8]]

Correct answer is: a) [[7, 0], [0, 8]]
Explanation: The code snippet imports the NumPy library and uses the np.diag() function to create a 2-dimensional diagonal array with the elements [7, 8]. The resulting array will have zeros in off-diagonal elements and the given elements on the diagonal. The np.diag() function creates a diagonal matrix from the given input array, placing the elements on the diagonal of the resulting matrix and filling other elements with zeros.

25). What is the output of the following code?

				
					import numpy as np
x = np.array([[5, 3],
              [7, 4]])
print("Sum of all the elements : ", np.sum(x))
				
			

a) 9
b) 14
c) 19
d) 22

Correct answer is: c) 19
Explanation: The given code imports the NumPy library and creates a 2-dimensional NumPy array `x` with the elements [[5, 3], [7, 4]]. The `np.sum()` function is then used to calculate the sum of all elements in the array `x`.

NumPy MCQ : Set 3

NumPy MCQ

1). What is the purpose of the np.random.rand() function?

a) Generates a random integer
b) Returns a random permutation of an array
c) Generates an array of random floats in the range [0, 1)
d) Calculates the cumulative sum of an array

Correct answer is: c) Generates an array of random floats in the range [0, 1)
Explanation: The np.random.rand() function generates an array of random floats in the half-open interval [0, 1). It takes the desired shape of the array as arguments.

2). How can you calculate the element-wise arccosine of a NumPy array?

a) np.arccos(arr)
b) np.arcsin(arr)
c) np.arctan(arr)
d) np.arccosh(arr)

Correct answer is: a) np.arccos(arr)
Explanation: The np.arccos() function in NumPy calculates the element-wise arccosine (inverse cosine) of a given array. It takes the array as an argument and returns a new array with the arccosine values.

3). What does the np.delete() function do?

a) Deletes an element from an array
b) Deletes a row or column from a 2-dimensional array
c) Deletes multiple elements from an array
d) Deletes a subarray from an array

Correct answer is: b) Deletes a row or column from a 2-dimensional array
Explanation: The np.delete() function in NumPy deletes a specified row or column from a 2-dimensional array. It takes the array and the index of the row or column to delete as arguments.

4). Which NumPy function is used to calculate the element-wise absolute difference between two arrays?

a) np.absdiff()
b) np.subtract()
c) np.diff()
d) np.absolute_diff()

Correct answer is: a) np.absdiff()
Explanation: The np.absdiff() function in NumPy calculates the element-wise absolute difference between two arrays. It takes the two arrays as arguments and returns a new array with the absolute differences.

5). What does the np.stack() function do?

a) Combines multiple arrays into a single array
b) Splits an array into multiple subarrays
c) Joins arrays along a new axis
d) Reverses the order of elements in an array

Correct answer is: c) Joins arrays along a new axis
Explanation: The np.stack() function in NumPy joins arrays along a new axis. It takes the arrays to stack as arguments and returns a new array with the arrays stacked along the specified axis.

6). How can you calculate the element-wise tangent of a NumPy array?

a) np.tan(arr)
b) np.arctan(arr)
c) np.sin(arr)
d) np.cos(arr)

Correct answer is: a) np.tan(arr)
Explanation: The np.tan() function in NumPy calculates the element-wise tangent of a given array. It takes the array as an argument and returns a new array with the tangent values.

7). What is the purpose of the np.full() function?

a) Generates an array filled with ones
b) Generates an array filled with zeros
c) Generates an array with a specified shape and fill value
d) Generates an array with random values

Correct answer is: c) Generates an array with a specified shape and fill value
Explanation: The np.full() function in NumPy generates an array with a specified shape and fill value. It takes the desired shape and the fill value as arguments and returns an array with the specified shape, filled with the fill value.

8). Which NumPy function is used to calculate the element-wise hyperbolic sine of an array?

a) np.sinh()
b) np.cosh()
c) np.tanh()
d) np.arcsinh()

Correct answer is: a) np.sinh()
Explanation: The np.sinh() function in NumPy calculates the element-wise hyperbolic sine of a given array. It takes the array as an argument and returns a new array with the hyperbolic sine values.

9). What does the np.argsort() function return?

a) The sorted array
b) The indices that would sort the array
c) The indices that sort the array in descending order
d) The indices of the maximum values in the array

Correct answer is: b) The indices that would sort the array
Explanation: The np.argsort() function returns the indices that would sort an array in ascending order. The initial array is unaltered.

10). How can you calculate the element-wise bitwise AND of two NumPy arrays?

a) np.bitwise_and(arr1, arr2)
b) np.bitwise_or(arr1, arr2)
c) np.bitwise_xor(arr1, arr2)
d) np.bitwise_not(arr1)

Correct answer is: a) np.bitwise_and(arr1, arr2)
Explanation: The np.bitwise_and() function in NumPy calculates the element-wise bitwise AND of two arrays. It takes the two arrays as arguments and returns a new array with the bitwise AND result.

11). What does the np.isinf() function do?

a) Checks if an array contains infinite values
b) Computes the element-wise exponential of an array
c) Calculates the cumulative sum of an array
d) Calculates the element-wise reciprocal of an array

Correct answer is: a) Checks if an array contains infinite values
Explanation: The np.isinf() function in NumPy checks if an array contains infinite values. It returns a Boolean array indicating which elements are infinite.

12). Which NumPy function is used to calculate the element-wise square root of an array?

a) np.square_root()
b) np.power(arr, 0.5)
c) np.sqrt(arr)
d) np.abs_sqrt(arr)

Correct answer is: c) np.sqrt(arr)
Explanation: The np.sqrt() function in NumPy calculates the element-wise square root of a given array. It takes the array as an argument and returns a new array with the square root values.

13). What is the purpose of the np.rot90() function?

a) It rotates an array by 90 degrees counter-clockwise
b) It rotates an array by 90 degrees clockwise
c) It reshapes an array to a specified shape
d) It transposes the rows and columns of an array

Correct answer is: a) It rotates an array by 90 degrees counter-clockwise
Explanation: The np.rot90() function in NumPy rotates an array by 90 degrees counter-clockwise. It takes the array and the number of rotations as arguments and returns a new array with the specified rotation.

14). Which NumPy function is used to calculate the element-wise exponential of an array?

a) np.exp()
b) np.power()
c) np.log()
d) np.square()

Correct answer is: a) np.exp()
Explanation: The np.exp() function in NumPy calculates the element-wise exponential of a given array. It takes the array as an argument and returns a new array with the exponential values.

15). What does the np.median() function return?

a) The mean of the array
b) The maximum value in the array
c) The minimum value in the array
d) The median of the array

Correct answer is: d) The median of the array
Explanation: The np.median() function in NumPy calculates the median value of an array. It takes the array as an argument and returns the median value.

16). How can you calculate the element-wise absolute difference between two NumPy arrays?

a) np.absdiff(arr1, arr2)
b) np.subtract(arr1, arr2)
c) np.add(arr1, arr2)
d) np.multiply(arr1, arr2)

Correct answer is: a) np.absdiff(arr1, arr2)
Explanation: The np.absdiff() function in NumPy calculates the element-wise absolute difference between two arrays. It takes the two arrays as arguments and returns a new array with the absolute differences.

17). Which NumPy function is used to calculate the element-wise arc tangent of two arrays?

a) np.arctan2()
b) np.arcsin()
c) np.arccos()
d) np.tan()

Correct answer is: a) np.arctan2()
Explanation: The np.arctan2() function in NumPy calculates the element-wise arc tangent of the quotient of two arrays. It takes the numerator array and the denominator array as arguments and returns a new array with the arc tangent values.

18). What is the purpose of the np.fliplr() function?

a) It flips the array vertically
b) It flips the array horizontally
c) It changes the shape of the array
d) It transposes the rows and columns of the array

Correct answer is: b) It flips the array horizontally
Explanation: The np.fliplr() function in NumPy flips the array horizontally. It takes the array as an argument and returns a new array with the columns reversed.

19). Which NumPy function is used to calculate the element-wise hyperbolic tangent of an array?

a) np.tanh()
b) np.sinh()
c) np.cosh()
d) np.arctanh()

Correct answer is: a) np.tanh()
Explanation: The np.tanh() function in NumPy calculates the element-wise hyperbolic tangent of a given array. It takes the array as an argument and returns a new array with the hyperbolic tangent values.

20). What does the np.vstack() function do?

a) Vertically stacks multiple arrays
b) Horizontally stacks multiple arrays
c) Splits an array into multiple subarrays
d) Calculates the dot product of two arrays

Correct answer is: a) Vertically stacks multiple arrays
Explanation: The np.vstack() function in NumPy vertically stacks multiple arrays. It takes the arrays to stack as arguments and returns a new array with the arrays stacked vertically.

21). What does the np.full_like() function do?

a) Creates a new array with the same shape and data type as a specified array, filled with a specified value
b) Creates a new array with the same shape as a specified array, filled with a specified value
c) Creates a new array with the same data type as a specified array, filled with a specified value
d) Creates a new array with the same shape and data type as a specified array, filled with ones

Correct answer is: a) Creates a new array with the same shape and data type as a specified array, filled with a specified value
Explanation: The np.full_like() function in NumPy creates a new array with the same shape and data type as a specified array. It takes the specified array and the fill value as arguments and returns a new array filled with the specified value.

22). How can you calculate the element-wise bitwise OR of two NumPy arrays?

a) np.bitwise_or(arr1, arr2)
b) np.bitwise_and(arr1, arr2)
c) np.bitwise_xor(arr1, arr2)
d) np.bitwise_not(arr1)

Correct answer is: a) np.bitwise_or(arr1, arr2)
Explanation: The np.bitwise_or() function in NumPy calculates the element-wise bitwise OR of two arrays. It takes the two arrays as arguments and returns a new array with the bitwise OR result.

23). What is the purpose of the np.linspace() function?

a) Generates a sequence of evenly spaced numbers over a specified range
b) Generates an array with random values
c) Generates an array filled with ones
d) Generates an array filled with zeros

Correct answer is: a) Generates a sequence of evenly spaced numbers over a specified range
Explanation: The np.linspace() function in NumPy generates a sequence of evenly spaced numbers over a specified range. It takes the start value, end value, and the number of elements as arguments.

24). What does the np.ptp() function calculate?

a) The sum of all elements in the array
b) The median of the array
c) The peak-to-peak (range) value of the array
d) The array’s total product of all of its elements

Correct answer is: c) The peak-to-peak (range) value of the array
Explanation: The np.ptp() function in NumPy calculates the peak-to-peak (range) value of an array. It takes the array as an argument and returns the difference between the maximum and minimum values.

25). Which NumPy function is used to calculate the element-wise sigmoid function of an array?

a) np.sinh()
b) np.cosh()
c) np.tanh()
d) np.expit()

Correct answer is: d) np.expit()
Explanation: The np.expit() function in NumPy calculates the element-wise sigmoid function of a given array. It takes the array as an argument and returns a new array with the sigmoid values.

NumPy MCQ : Set 2

NumPy MCQ

1). What does the `np.unique()` function do?

a) Removes duplicate elements from an array
b) Returns the element-wise square of an array
c) Computes the cumulative sum of an array
d) Performs element-wise division of two arrays

Correct answer is: a) Removes duplicate elements from an array
Explanation: The `np.unique()` function in NumPy returns the sorted, unique values in an array, removing any duplicate elements.

2). How can you calculate the element-wise sine of a NumPy array?

a) np.sin(arr)
b) np.cos(arr)
c) np.tan(arr)
d) All of the above

Correct answer is: a) np.sin(arr)
Explanation: The `np.sin()` function in NumPy calculates the element-wise sine of a given array. It takes the array as an argument and returns a new array with the sine values.

3). Which NumPy function is used to compute the covariance matrix of a set of variables?

a) np.cov()
b) np.correlate()
c) np.corrcoef()
d) np.corr()

Correct answer is: a) np.cov()
Explanation: The `np.cov()` function in NumPy is used to compute the covariance matrix of a set of variables. It takes the variables as arguments and returns the covariance matrix.

4). What does the `np.concatenate()` function do?

a) Combines multiple arrays into a single array
b) Splits an array into multiple subarrays
c) Reverses the order of elements in an array
d) Sorts the elements of an array in ascending order

Correct answer is: a) Combines multiple arrays into a single array
Explanation: The `np.concatenate()` function in NumPy is used to combine multiple arrays along a specified axis, resulting in a single array.

5). How can you calculate the element-wise logarithm of a NumPy array?

a) np.log(arr)
b) np.exp(arr)
c) np.power(arr, 2)
d) np.sqrt(arr)

Correct answer is: a) np.log(arr)
Explanation: The `np.log()` function in NumPy calculates the element-wise natural logarithm (base e) of a given array. It takes the array as an argument and returns a new array with the logarithmic values.

6). What is the purpose of the `np.where()` function?

a) It finds the maximum value in an array
b) It replaces elements in an array based on a condition
c) It calculates the element-wise sum of two arrays
d) It arranges array elements in decreasing order.

Correct answer is: b) It replaces elements in an array based on a condition
Explanation: The `np.where()` function in NumPy is used to replace elements in an array based on a specified condition. It takes the condition, the values to replace when the condition is True, and the values to replace when the condition is False.

7). What does the `np.pad()` function do?

a) Adds padding to an array
b) Removes padding from an array
c) Changes the shape of an array
d) Calculates the element-wise difference of two arrays

Correct answer is: a) Adds padding to an array
Explanation: The `np.pad()` function in NumPy is used to add padding to an array. It takes the array, the pad widths, and the padding mode as arguments, and returns a new array with the specified padding.

8). How can you calculate the element-wise product of two NumPy arrays?

a) np.multiply(arr1, arr2)
b) np.dot(arr1, arr2)
c) np.cross(arr1, arr2)
d) np.divide(arr1, arr2)

Correct answer is: a) np.multiply(arr1, arr2)
Explanation: The `np.multiply()` function in NumPy calculates the element-wise product of two arrays. It takes the two arrays as arguments and returns a new array with the product of corresponding elements.

9). What does the `np.nan` value represent in NumPy?

a) Not a number
b) Null or missing value
c) Infinite value
d) Negative infinity

Correct answer is: a) Not a number
Explanation: The `np.nan` value in NumPy represents a “Not a Number” value, typically used to denote missing or undefined data.

10). Which NumPy function is used to calculate the element-wise absolute difference between two arrays?

a) np.abs()
b) np.diff()
c) np.subtract()
d) np.absolute()

Correct answer is: d) np.absolute()
Explanation: The `np.absolute()` function in NumPy calculates the element-wise absolute difference between two arrays. It takes the two arrays as arguments and returns a new array with the absolute differences.

11). What is the purpose of the `np.clip()` function?

a) It rounds the elements of an array to the nearest integer
b) It limits the values of an array to a specified range
c) It calculates the cumulative product of an array
d) It computes the element-wise exponential of an array

Correct answer is: b) It limits the values of an array to a specified range
Explanation: The `np.clip()` function in NumPy limits the values of an array to a specified range. It takes the array, the minimum value, and the maximum value as arguments, and returns a new array with the values clipped to the specified range.

12). How can you calculate the element-wise division of two NumPy arrays?

a) np.divide(arr1, arr2)
b) np.multiply(arr1, arr2)
c) np.power(arr1, arr2)
d) np.dot(arr1, arr2)

Correct answer is: a) np.divide(arr1, arr2)
Explanation: The `np.divide()` function in NumPy calculates the element-wise division of two arrays. It takes the two arrays as arguments and returns a new array with the quotient of corresponding elements.

13). What does the `np.around()` function do?

a) The array’s elements are rounded to the nearest integer.
b) Rounds the elements of an array to a specified number of decimals
c) Returns the integer part of the elements in an array
d) Calculates the element-wise absolute value of an array

Correct answer is: b) Rounds the elements of an array to a specified number of decimals
Explanation: The `np.around()` function in NumPy rounds the elements of an array to a specified number of decimals. It takes the array and the number of decimals as arguments and returns a new array with the rounded values.

14). Which NumPy function is used to calculate the element-wise arctangent of an array?

a) np.arctan()
b) np.arcsin()
c) np.arccos()
d) np.arctan2()

Correct answer is: a) np.arctan()
Explanation: The `np.arctan()` function in NumPy calculates the element-wise arctangent (inverse tangent) of a given array. It takes the array as an argument and returns a new array with the arctangent values.

15). What is the purpose of the `np.isclose()` function?

a) It checks if two arrays are element-wise equal
b) It calculates the element-wise sum of two arrays
c) It computes the element-wise exponential of an array
d) It rounds the elements of an array to the nearest integer

Correct answer is: a) It checks if two arrays are element-wise equal
Explanation: The `np.isclose()` function in NumPy checks if two arrays are element-wise equal within a specified tolerance. It returns a Boolean array indicating the element-wise equality.

16). How can you calculate the element-wise power of a NumPy array?

a) np.power(arr, exponent)
b) np.sqrt(arr)
c) np.abs(arr)
d) np.log(arr)

Correct answer is: a) np.power(arr, exponent)
Explanation: The `np.power()` function in NumPy calculates the element-wise power of a given array with a specified exponent. It takes the array and the exponent as arguments and returns a new array with the powered values.

17). What does the `np.mean()` function return if the array contains NaN values?

a) NaN
b) 0
c) The mean of the non-NaN values
d) An error is raised

Correct answer is: c) The mean of the non-NaN values
Explanation: The `np.mean()` function ignores NaN values when calculating the mean of an array. It returns the mean of the non-NaN values in the array.

18). Which NumPy function is used to calculate the element-wise cosine of an array?

a) np.sin()
b) np.cos()
c) np.tan()
d) np.arccos()

Correct answer is: b) np.cos()
Explanation: The `np.cos()` function in NumPy calculates the element-wise cosine of a given array. It takes the array as an argument and returns a new array with the cosine values.

19). What is the purpose of the `np.zeros()` function?

a) It generates an array filled with zeros
b) It generates an array filled with ones
c) It generates an array filled with random values
d) It generates an array with a specified shape and values

Correct answer is: a) It generates an array filled with zeros
Explanation: The `np.zeros()` function in NumPy generates an array filled with zeros. It takes the desired shape of the array as an argument and returns an array with the specified shape and filled with zeros.

20). Which of the following functions is used to find the variance of an array?

a) var()
b) variance()
c) numpy.var()
d) numpy.variance()

Correct answer is: c) numpy.var()
Explanation: The var() function is a NumPy function that returns the variance of an array. The variance() function is a Python function that returns the variance of a sequence. The numpy.var() and numpy.variance() functions are equivalent.

21). How can you calculate the element-wise floor division of two NumPy arrays?

a) np.floor_divide(arr1, arr2)
b) np.divide(arr1, arr2)
c) np.multiply(arr1, arr2)
d) np.subtract(arr1, arr2)

Correct answer is: a) np.floor_divide(arr1, arr2)
Explanation: The `np.floor_divide()` function in NumPy calculates the element-wise floor division of two arrays. It takes the two arrays as arguments and returns a new array with the quotient of corresponding elements, rounded down to the nearest integer.

22). Which NumPy function is used to calculate the element-wise logarithm base 10 of an array?

a) np.log10()
b) np.log2()
c) np.log()
d) np.log1p()

Correct answer is: a) np.log10()
Explanation: The np.log10() function in NumPy calculates the element-wise logarithm base 10 of a given array. It takes the array as an argument and returns a new array with the logarithm values.

23). What does the np.ravel() function do?

a) Reshapes an array to a specified shape
b) Returns a sorted copy of an array
c) Flattens an array into a 1-dimensional array
d) Rounds the elements of an array to the nearest integer

Correct answer is: c) Flattens an array into a 1-dimensional array
Explanation: The np.ravel() function in NumPy flattens an array into a 1-dimensional array by concatenating all elements. It returns a new array with the flattened structure.

24). How can you calculate the element-wise square of a NumPy array?

a) np.square(arr)
b) np.power(arr, 2)
c) np.sqrt(arr)
d) np.abs(arr)

Correct answer is: a) np.square(arr)
Explanation: The np.square() function in NumPy calculates the element-wise square of a given array. It takes the array as an argument and returns a new array with the squared values.

25). Which NumPy function is used to calculate the element-wise inverse of an array?

a) np.inverse()
b) np.reciprocal()
c) np.divide(1, arr)
d) np.divide(arr, 1)

Correct answer is: b) np.reciprocal()
Explanation: The np.reciprocal() function in NumPy calculates the element-wise reciprocal (inverse) of a given array. It takes the array as an argument and returns a new array with the reciprocal values.

NumPy MCQ : Set 1

NumPy MCQ

1). What is NumPy?

a) A programming language
b) A numerical computing library for Python
c) A data visualization library
d) A machine learning framework

Correct answer is: b) A numerical computing library for Python
Explanation: NumPy is a powerful library for numerical computing in Python. It offers support for sizable, multidimensional arrays and matrices, as well as a range of mathematical operations for effectively using these arrays.

2). Which of the following is not a key feature of NumPy?

a) N-dimensional array object
b) Broadcasting functions
c) Linear algebra routines
d) Data visualization tools

Correct answer is: d) Data visualization tools
Explanation: While NumPy provides efficient numerical computing capabilities, it does not include built-in data visualization tools. For data visualization, libraries like Matplotlib or Seaborn are commonly used in conjunction with NumPy.

3). What is the main benefit of using NumPy arrays over Python lists?

a) NumPy arrays can store elements of different data types
b) NumPy arrays consume less memory
c) NumPy arrays can have variable lengths
d) NumPy arrays support dynamic resizing

Correct answer is: b) NumPy arrays consume less memory
Explanation: NumPy arrays are more memory-efficient compared to Python lists because they store elements of the same data type in a contiguous block of memory. This allows for better performance and reduced memory consumption.

4). Which of the following statements is true about NumPy arrays?

a) NumPy arrays are immutable
b) NumPy arrays can have varying lengths
c) NumPy arrays can only store numeric data types
d) NumPy arrays are resizable

Correct answer is: c) NumPy arrays can only store numeric data types
Explanation: NumPy arrays are designed to work with homogeneous data, meaning they can only store elements of a single data type, such as integers, floats, or complex numbers.

5). How can you create a NumPy array from a Python list?

a) np.array(list_name)
b) numpy.array(list_name)
c) np.ndarray(list_name)
d) numpy.ndarray(list_name)

Correct answer is: a) np.array(list_name)
Explanation: The `np.array()` function in NumPy can be used to create an array from a Python list. It accepts the argument list and returns a NumPy array.

6). Which NumPy function is used to calculate the mean of an array?

a) np.mean()
b) np.average()
c) np.median()
d) np.sum()

Correct answer is: a) np.mean()
Explanation: The `np.mean()` function in NumPy is used to calculate the arithmetic mean of an array. It takes the array as an argument and returns the mean value.

7). Which NumPy function is used to find the maximum value in an array?

a) np.max()
b) np.maximum()
c) np.argmax()
d) np.amax()

Correct answer is: a) np.max()
Explanation: The `np.max()` function is used to find the maximum value in a NumPy array. It takes the array as an argument and returns the maximum value.

8). How can you reshape a 1-dimensional array into a 2-dimensional array with two rows?

a) arr.reshape((2, 2))
b) arr.reshape((2, -1))
c) arr.reshape((1, 2))
d) arr.reshape((2,))

Correct answer is: b) arr.reshape((2, -1))
Explanation: The `reshape()` function in NumPy can be used to change the shape of an array. By specifying the target shape as `(2, -1)`, NumPy automatically infers the number of columns based on the size of the array.

9). What does the `np.linspace()` function do?

a) Generates a sequence of equally spaced values
b) Returns a random permutation of an array
c) Calculates the logarithm of each element in an array
d) Performs element-wise multiplication of two arrays

Correct answer is: a) Generates a sequence of equally spaced values
Explanation: The `np.linspace()` function generates a sequence of equally spaced values over a specified interval. It takes the start, end, and the number of values to generate as arguments.

10). How can you concatenate two NumPy arrays vertically?

a) np.concatenate((arr1, arr2), axis=0)
b) np.vstack((arr1, arr2))
c) np.append(arr1, arr2, axis=0)
d) All of the above

Correct answer is: d) All of the above
Explanation: All the provided options can be used to concatenate two NumPy arrays vertically, resulting in an array that has the rows from both arrays stacked on top of each other.

11). What does the `np.random.randint()` function do?

a) Generates a random integer
b) Returns a random permutation of an array
c) Generates an array of random integers
d) Calculates the cumulative sum of an array

Correct answer is: c) Generates an array of random integers
Explanation: The `np.random.randint()` function generates an array of random integers from a specified low (inclusive) to high (exclusive) range. The function takes the low, high, and the size of the array as arguments.

12). Which NumPy function is used to compute the dot product of two arrays?

a) np.dot()
b) np.cross()
c) np.inner()
d) np.outer()

Correct answer is: a) np.dot()
Explanation: The `np.dot()` function in NumPy is used to compute the dot product of two arrays. It performs matrix multiplication if the inputs are 2-dimensional arrays.

13). What is the purpose of broadcasting in NumPy?

a) To perform element-wise operations on arrays of different shapes
b) To adjust the shape of an array to match another array
c) To randomly shuffle the elements of an array
d) To compute the cumulative sum of an array

Correct answer is: a) To perform element-wise operations on arrays of different shapes
Explanation: Broadcasting in NumPy allows for performing element-wise operations on arrays of different shapes by automatically adjusting their shapes to be compatible.

14). How can you save a NumPy array to a file?

a) np.save(file_name, arr)
b) np.savez(file_name, arr)
c) np.savetxt(file_name, arr)
d) All of the above

Correct answer is: d) All of the above
Explanation: All the provided options can be used to save a NumPy array to a file. `np.save()` saves the array in binary format, `np.savez()` saves multiple arrays in an uncompressed archive, and `np.savetxt()` saves the array as a text file.

15). How can you load a NumPy array from a file?

a) np.load(file_name)
b) np.loadtxt(file_name)
c) np.fromfile(file_name)
d) All of the above

Correct answer is: a) np.load(file_name)
Explanation: The `np.load()` function in NumPy is used to load a saved NumPy array from a file. It takes the file name as an argument and returns the loaded array.

16). Which NumPy function is used to calculate the standard deviation of an array?

a) np.std()
b) np.var()
c) np.mean()
d) np.median()

Correct answer is: a) np.std()
Explanation: The `np.std()` function in NumPy calculates the standard deviation of an array, which measures the spread of values around the mean. It takes the array as an argument and returns the standard deviation.

17). What does the `np.eye()` function do?

a) Generates a diagonal array
b) Returns the element-wise exponential of an array
c) Calculates the inverse of a matrix
d) Performs element-wise division of two arrays

Correct answer is: a) Generates a diagonal array
Explanation: A two-dimensional array with ones on the diagonal and zeros elsewhere is produced using the ‘np.eye()’ function. It takes the number of rows as an argument, and by default, it creates a square array.

18). Which NumPy function is used to find the indices of the sorted elements in an array?

a) np.sort()
b) np.argsort()
c) np.sort_indices()
d) np.searchsorted()

Correct answer is: b) np.argsort()
Explanation: The `np.argsort()` function in NumPy returns the indices that would sort an array in ascending order. It takes the array as an argument and returns an array of indices.

19). What is the purpose of the `axis` parameter in NumPy functions?

a) It specifies the data type of the array
b) It controls the dimensionality of the array
c) It determines the axis along which the operation is applied
d) It defines the shape of the resulting array

Correct answer is: c) It determines the axis along which the operation is applied
Explanation: The `axis` parameter in NumPy functions is used to specify the axis along which the operation is applied. It can be used to perform operations across rows, columns, or other dimensions of the array.

20). How can you calculate the element-wise square root of a NumPy array?

a) np.sqrt(arr)
b) np.square(arr)
c) np.power(arr, 0.5)
d) All of the above

Correct answer is: a) np.sqrt(arr)
Explanation: The `np.sqrt()` function in NumPy calculates the element-wise square root of a given array. It takes the array as an argument and returns a new array with the square root of each element.

21). What does the `np.transpose()` function do?

a) Flips the array vertically
b) Flips the array horizontally
c) Changes the shape of the array
d) Interchanges the rows and columns of the array

Correct answer is: d) Interchanges the rows and columns of the array
Explanation: The `np.transpose()` function in NumPy returns a view of the array with the rows and columns interchanged. The initial array is unaltered.

22). What is the purpose of the `np.meshgrid()` function?

a) It generates a grid of coordinates based on input arrays
b) It computes the element-wise product of two arrays
c) It calculates the dot product of two arrays
d) It reshapes an array into a specified shape

Correct answer is: a) It generates a grid of coordinates based on input arrays
Explanation: The `np.meshgrid()` function is used to generate a grid of coordinates based on the input arrays. It takes two or more arrays representing the coordinates and returns coordinate matrices for each pair of input arrays.

23). Which NumPy function is used to calculate the element-wise absolute value of an array?

a) np.abs()
b) np.absolute()
c) np.absolutes()
d) np.magnitude()

Correct answer is: a) np.abs()
Explanation: The `np.abs()` function in NumPy calculates the element-wise absolute value of a given array. It takes the array as an argument and returns a new array with the absolute values.

24). What does the `np.histogram()` function do?

a) Calculates a data set’s histogram
b) Generates a random array with a specified shape
c) Calculates the cumulative sum of an array
d) Performs element-wise multiplication of two arrays

Correct answer is: a) Calculates a data set’s histogram
Explanation: The `np.histogram()` function computes the histogram of a set of data. It takes the data and the number of bins as arguments and returns an array of bin counts and an array of bin edges.

25). How can you calculate the element-wise exponential of a NumPy array?

a) np.exp(arr)
b) np.power(arr, 2)
c) np.log(arr)
d) np.sin(arr)

Correct answer is: a) np.exp(arr)
Explanation: The `np.exp()` function in NumPy calculates the element-wise exponential of a given array. It takes the array as an argument and returns a new array with the exponential values.

Python Pandas MCQ : Set 6

Python Pandas MCQ

1). What is the output of the following code?

				
					import pandas as pd
import numpy as np
d = {'Sr.no.': [1, 2, 3, 4], 'Name': ['Alex', 'John', 'Peter', 'Klaus'], 'Age': [30, np.nan, 29, np.nan]}
df = pd.DataFrame(d)
print("Nan values in the dataframe: ", df.isnull().values.sum())
				
			

a) 0
b) 1
c) 2
d) 3

Correct answer is: c) 2
Explanation: The code creates a DataFrame `df` with four rows and three columns (‘Sr.no.’, ‘Name’, and ‘Age’). The ‘Age’ column contains two NaN (Not a Number) values. The `isnull().values` method is used to create a boolean array where True represents the presence of a NaN value, and False represents a non-NaN value. The `sum()` function is then applied to this boolean array to count the total number of True (i.e., NaN) values. In this case, the output will be `2`, indicating that there are two NaN value in the DataFrame.

2). What is the purpose of the following code?

				
					import pandas as pd
d = {'Sr.no.': [1, 2, 3, 4], 'Name': ['Alex', 'John', 'Peter', 'Klaus'], 'Age': [30, 27, 29, 33], 'Salary': [50000, 65000, 58000, 66000]}
df = pd.DataFrame(d)
df = df.sample(frac=1)
print("\nNew DataFrame:")
print(df)

				
			

a) To create a DataFrame with given data and shuffle its rows randomly.
b) To sort the DataFrame rows in ascending order based on a specified column.
c) To remove duplicate rows from the DataFrame.
d) To convert the DataFrame into a NumPy array.

Correct answer is: a) To create a DataFrame with given data and shuffle its rows randomly.
Explanation: The given code performs the following actions:
1. Import the Pandas library as `pd`.
2. Create a Python dictionary `d` containing four columns: ‘Sr.no.’, ‘Name’, ‘Age’, and ‘Salary’.
3. Use the dictionary to create a DataFrame `df`.
4. `df.sample(frac=1)` is used to shuffle the rows of the DataFrame randomly. The parameter `frac=1` specifies that the entire DataFrame should be sampled, effectively shuffling the entire DataFrame.
5. Finally, the shuffled DataFrame is printed with the message “New DataFrame:”.

3). What is the output of the following code?

				
					import pandas as pd
d = {'Rank':[1,2,3,4],'Name':['Alex','John','Peter','Klaus'],'Age':[30,27,29,33],'Salary':[50000,65000,58000,66000]}
df = pd.DataFrame(d)
df = df.rename(columns = {'Rank':'Sr.no.'})
print("New: \n",df)
				
			

a)
New:
Sr.no. Name Age Salary
0 1 Alex 30 50000
1 2 John 27 65000
2 3 Peter 29 58000
3 4 Klaus 33 66000

b)
New:
Rank Name Age Salary
0 1 Alex 30 50000
1 2 John 27 65000
2 3 Peter 29 58000
3 4 Klaus 33 66000

c)
New:
Sr.no. Name Age Salary
0 1 Alex 30 50000
1 2 John 27 65000
2 3 Peter 29 58000
3 4 Klaus 33 66000

d)
New:
Sr.no. Name Age Salary
1 1 Alex 30 50000
2 2 John 27 65000
3 3 Peter 29 58000
4 4 Klaus 33 66000

Correct answer is: b)
New:
Rank Name Age Salary
0 1 Alex 30 50000
1 2 John 27 65000
2 3 Peter 29 58000
3 4 Klaus 33 66000
Explanation: The code creates a DataFrame `df` using the provided dictionary `d`, which contains columns ‘Rank’, ‘Name’, ‘Age’, and ‘Salary’. The `rename()` function is then used to rename the ‘Rank’ column to ‘Sr.no.’. Finally, the modified DataFrame is printed using the `print()` function.

4). What is the output of the following code?

				
					import pandas as pd
d = {'Sr.no.': [1, 2, 3, 4], 'Name': ['Alex', 'John', 'Peter', 'Klaus'], 'Age': [30, 27, 29, 33], 'Salary': [50000, 65000, 58000, 66000]}
df = pd.DataFrame(d)
name_list = df['Name'].tolist()
print("List of names: ", name_list)
				
			

b) List of names: ‘Alex’, ‘John’, ‘Peter’, ‘Klaus’
c) List of names: ‘Alex’ ‘John’ ‘Peter’ ‘Klaus’
d) List of names: [‘Alex’, ‘John’, ‘Peter’]

Correct answer is: a) List of names: [‘Alex’, ‘John’, ‘Peter’, ‘Klaus’]
Explanation: The code creates a DataFrame named ‘df’ with columns ‘Sr.no.’, ‘Name’, ‘Age’, and ‘Salary’. Then, it extracts the ‘Name’ column using `df[‘Name’]`, converts it to a Python list using the `tolist()` method, and assigns it to the variable ‘name_list’. Finally, it prints the list of names. The correct output is “List of names: [‘Alex’, ‘John’, ‘Peter’, ‘Klaus’]”. Option a represents the correct list of names. Options b and c are incorrect because they use incorrect quotation marks and do not form a valid Python list. Option d is correct but contains unnecessary square brackets around the list, which are not present in the actual output.

5). What is the output of the following code?

				
					import pandas as pd
d = {'Sr.no.':[1,2,3,4],'Name':['Alex','John','Peter','Klaus'],'Age':[30,27,29,33],'Salary':[50000,65000,58000,66000]}
df = pd.DataFrame(d)
print("Row where Salary has maximum value:")
print(df['Salary'].argmax())
				
			

a) 0
b) 1
c) 2
d) 3

Correct answer is: d) 3
Explanation: The code creates a DataFrame `df` with four columns: ‘Sr.no.’, ‘Name’, ‘Age’, and ‘Salary’. It then prints the row where the ‘Salary’ column has the maximum value using the `argmax()` method) The `argmax()` method returns the index label (row number) of the first occurrence of the maximum value in the Series. In this case, the ‘Salary’ column has the maximum value of 66000 at index 3 (row 4, considering zero-based indexing), and hence the output of the code will be 3.

6). What is the output of the following code?

				
					import pandas as pd
d = {'Sr.no.':[1,2,3,4],'Name':['Alex','John','Peter','Klaus'],'Age':[30,27,29,33],'Salary':[50000,65000,58000,66000]}
df = pd.DataFrame(d)
print("Row where Salary has minimum value:")
print(df['Salary'].argmin())
				
			

a) 0
b) 1
c) 2
d) 3

Correct answer is: a) 0
Explanation: The code creates a DataFrame `df` with columns ‘Sr.no.’, ‘Name’, ‘Age’, and ‘Salary’. It then prints the row number where the ‘Salary’ column has the minimum value using the `argmin()` function. The `argmin()` function returns the index (row number) of the first occurrence of the minimum value in the Series. In this case, the ‘Salary’ column has the values [50000, 65000, 58000, 66000]. The minimum value in the ‘Salary’ column is 50000, which occurs in the first row (index 0) with the value ‘Alex’.

7). What is the output of the following code?

				
					import pandas as pd
import numpy as np
d = {'Sr.no.': [1, 2, 3, 4], 'Name': ['Alex', 'John', 'Peter', 'Klaus'], 'Age': [30, 27, 29, 33], 'Salary': [50000, 65000, 58000, 66000]}
df = pd.DataFrame(d)

for i in ["Company", 'Name']:
    if i in df.columns:
        print(f"{i} is present in DataFrame.")
    else:
        print(f"{i} is not present in DataFrame.")
				
			

a) Company is present in DataFrame.
Name is present in DataFrame.
b) Company is present in DataFrame.
Name is not present in DataFrame.
c) Company is not present in DataFrame.
Name is present in DataFrame.
d) Company is not present in DataFrame.
Name is not present in DataFrame.

Correct answer is: c) Company is not present in DataFrame.
Name is present in DataFrame.
Explanation: In the given code, a DataFrame `df` is created from the provided dictionary `d`. The DataFrame has columns: ‘Sr.no.’, ‘Name’, ‘Age’, and ‘Salary’. The code then iterates through the list `[“Company”, ‘Name’]` and checks if each item is present in the DataFrame’s columns using the `in` operator.

8). What is the output of the following code?

				
					import pandas as pd
d = {'Sr.no.':[1,2,3,4],'Name':['Alex','John','Peter','Klaus'],'Age':[30,27,29,33],'Salary':[50000,65000,58000,66000]}
df = pd.DataFrame(d)
print(df.dtypes)
				
			

a) Sr.no. int64
Name object
Age int64
Salary int64
dtype: object

b) Sr.no. int32
Name object
Age int32
Salary int32
dtype: object

c) Sr.no. int32
Name object
Age int64
Salary int32
dtype: object

d) Sr.no. int64
Name object
Age int32
Salary int64
dtype: object

Correct answer is: a) Sr.no. int64
Name object
Age int64
Salary int64
dtype: object
Explanation: The given code creates a DataFrame named `df` with four columns: ‘Sr.no.’, ‘Name’, ‘Age’, and ‘Salary’. The `print(df.dtypes)` statement will display the data types of each column in the DataFrame. In the DataFrame, the ‘Sr.no.’, ‘Age’, and ‘Salary’ columns contain integer values, so their data types are represented as `int64`, which is the standard 64-bit integer data type. The ‘Name’ column contains strings, so its data type is represented as `object`.

9). What is the output of the following code?

				
					import pandas as pd
l = [['Virat','cricket'],['Messi','football']]
df = pd.DataFrame(l)
print(df)
				
			

a)
0 1
0 Virat cricket
1 Messi football

b)
0 1
0 Virat
1 Messi
2 cricket
3 football

c)
0
0 Virat
1 Messi
2 cricket
3 football

d)
0 1
0 1 2
1 1 2

Correct answer is: a)
0 1
0 Virat cricket
1 Messi football
Explanation: The given code snippet imports the Pandas library as `pd` and creates a DataFrame `df` from a list of lists `l`. The DataFrame `df` contains two rows and two columns with data as follows:
0 1
0 Virat cricket
1 Messi football

10). What is the output of the following code?

				
					import pandas as pd
d = {'Sr.no.':[1,2,3,4],'Name':['Alex','John','Peter','Klaus'],'Age':[30,27,29,33],'Salary':[50000,65000,58000,66000]}
df = pd.DataFrame(d)
index = df.columns.get_loc('Age')
print("Index no of age column: ", index)
				
			

a) Index no of age column: 2
b) Index no of age column: 3
c) Index no of age column: 1
d) Index no of age column: 0

Correct answer is: a) Index no of age column: 2
Explanation: The code first imports the pandas library as pd. Then, it creates a DataFrame `df` with columns ‘Sr.no.’, ‘Name’, ‘Age’, and ‘Salary’. Next, it retrieves the index of the column ‘Age’ using the `get_loc()` method, and the index is assigned to the variable `index`. Finally, it prints the result as “Index no of age column: ” followed by the value of `index`. Since ‘Age’ is the third column in the DataFrame (index 2, as Python uses 0-based indexing), the correct output will be “Index no of age column: 2”. Thus, option a is the correct answer.

11). What is the purpose of the following code?

				
					import pandas as pd
d = {'Sr.no.': [1, 2, 3, 4], 'Name': ['Alex', 'John', 'Peter', 'Klaus'], 'Age': [30, 27, 29, 33], 'Salary': [50000, 65000, 58000, 66000]}
df = pd.DataFrame(d)
print("After removing first 2 rows of the DataFrame:")
df1 = df.iloc[2:]
print(df1)
				
			

a) It calculates the average age of all employees in the DataFrame.
b) It prints the original DataFrame without any modifications.
c) It creates a new DataFrame `df1` by removing the first 2 rows from the original DataFrame `df`.
d) It sorts the DataFrame in ascending order based on the ‘Age’ column.

Correct answer is: c) It creates a new DataFrame `df1` by removing the first 2 rows from the original DataFrame `df`.
Explanation: The code starts by importing the Pandas library and creating a DataFrame `df` with columns: ‘Sr.no.’, ‘Name’, ‘Age’, and ‘Salary’. The DataFrame contains information about four individuals, including their serial numbers, names, ages, and salaries. The next line of code prints a message indicating that the DataFrame will be modified by removing the first 2 rows. The subsequent line of code creates a new DataFrame `df1` by using the `iloc[2:]` attribute, which slices the original DataFrame `df` starting from the third row (index 2) to the end) This effectively removes the first two rows from `df`, and the resulting DataFrame is assigned to `df1`. Finally, the code prints the newly created DataFrame `df1`, which contains the data for the last two individuals in the original DataFrame `df`.

12). What is the purpose of the following code?

				
					import pandas as pd
d = {'Sr.no.':[1,2,3,4],'Name':['Alex','John','Peter','Klaus'],'Age':[30,27,29,33],'Salary':[50000,65000,58000,66000]}
df = pd.DataFrame(d)
print("Reverse row order:")
print(df.loc[::-1])
				
			

a) It creates a DataFrame `df` with columns ‘Sr.no.’, ‘Name’, ‘Age’, and ‘Salary’ and displays it in reverse order.
b) It reverses the order of the rows in the existing DataFrame `df` and displays the result.
c) It creates a new DataFrame by extracting rows from `df` in reverse order and displays it.
d) It sorts the DataFrame `df` in descending order based on the index labels and displays the result.

Correct answer is: b) It reverses the order of the rows in the existing DataFrame `df` and displays the result.
Explanation: The given code snippet uses the Pandas library to create a DataFrame `df` from a dictionary `d`, containing columns ‘Sr.no.’, ‘Name’, ‘Age’, and ‘Salary’. The DataFrame is then printed with a reverse row order using the `loc` attribute and the slicing notation `[::-1]`. Option b is the correct answer because the `df.loc[::-1]` syntax is used to reverse the order of rows in the DataFrame `df`. The result will display the DataFrame with rows in reverse order, meaning the last row becomes the first, the second-last row becomes the second, and so on.

13). What is the purpose of the following code?

				
					import pandas as pd
d = {'Sr.no.': [1, 2, 3, 4], 'Name': ['Alex', 'John', 'Peter', 'Klaus'], 'Age': [30, 27, 29, 33], 'Salary': [50000, 65000, 58000, 66000]}
df = pd.DataFrame(d)
print("Reverse column order:")
print(df.loc[:, ::-1])
				
			

a) It drops the ‘Sr.no.’ column from the DataFrame.
b) It sorts the DataFrame in reverse alphabetical order based on the column names.
c) It reverses the order of rows in the DataFrame.
d) It reverses the order of columns in the DataFrame.

Correct answer is: d) It reverses the order of columns in the DataFrame.
Explanation: The given code snippet imports the Pandas library as ‘pd’, creates a DataFrame ‘df’ with four columns (Sr.no., Name, Age, and Salary), and then prints the DataFrame in reverse column order using the `loc[:, ::-1]` slicing. `df.loc[:, ::-1]` is a pandas DataFrame indexing operation that selects all rows (`:`) and reverses the order of columns (`::-1`). As a result, the DataFrame ‘df’ will be printed with the columns in reverse order.

14). What is the purpose of the following code?

				
					import pandas as pd
d = {'Sr.no.':[1,2,3,4],'Name':['Alex','John','Peter','Klaus'],'Age':[30,27,29,33],'Salary':[50000,65000,58000,66000]}
df = pd.DataFrame(d)
print("Select string columns")
print(df.select_dtypes(include = "object"))
				
			

a) To create a DataFrame with columns for Sr.no., Name, Age, and Salary
b) To filter and display only the columns with string data type
c) To calculate the mean salary of the employees
d) To calculate the median age of the employees

Correct answer is: b) To filter and display only the columns with string data type
Explanation: The provided code performs the following tasks:
1. Import the Pandas library and assign the DataFrame with columns “Sr.no.”, “Name”, “Age”, and “Salary” to the variable `df`.
2. Prints the message “Select string columns”.
3. Uses the `select_dtypes()` function to filter and display only the columns with the data type “object” (i.e., columns with string values) from the DataFrame `df`.

15). What is the output of the following code?

				
					import pandas as pd
d = {'Name': ['Kate', 'Jason', 'ROBERT', 'MARK', 'Dwyane']}
df = pd.DataFrame(d)
df['Uppercase'] = list(map(lambda x: x.isupper(), df['Name']))
print(df)
				
			

a)
Name Uppercase
0 Kate False
1 Jason False
2 ROBERT True
3 MARK True
4 Dwyane False

b)
Name Uppercase
0 Kate False
1 Jason False
2 ROBERT False
3 MARK False
4 Dwyane False

c)
Name Uppercase
0 Kate True
1 Jason True
2 ROBERT True
3 MARK True
4 Dwyane True

d)
Name Uppercase
0 Kate False
1 Jason True
2 ROBERT False
3 MARK True
4 Dwyane False

Correct answer is: a)
Name Uppercase
0 Kate False
1 Jason False
2 ROBERT True
3 MARK True
4 Dwyane False
Explanation: The given code creates a DataFrame `df` with a ‘Name’ column containing five names. It then adds a new column ‘Uppercase’ to the DataFrame, which is populated with True or False based on whether each name in the ‘Name’ column contains all uppercase letters.

16). What is the purpose of the following code?

				
					import pandas as pd
d = {'Name':['kate','jason','ROBERT','MARK','dwyane']}
df = pd.DataFrame(d)
df['Lowercase'] = list(map(lambda x: x.islower(), df['Name']))
print(df)
				
			

Correct answer is: c) To add a new column ‘Lowercase’ indicating if each name is lowercase or not
Explanation: The purpose of this code is to add a new column named ‘Lowercase’ to the DataFrame ‘df’. The ‘Lowercase’ column is created using the `map()` function and a lambda function that checks if each name in the ‘Name’ column is lowercase or not. The resulting Boolean values (True or False) are added to the ‘Lowercase’ column. The code then prints the updated DataFrame, which includes the new ‘Lowercase’ column indicating whether each name is lowercase or not.

17). What is the purpose of the following code?

				
					import pandas as pd
d = {'Marks':['Pass','88','First Class','90','Distinction']}
df = pd.DataFrame(d)
df['Numeric'] = list(map(lambda x: x.isdigit(), df['Marks']))
print(df)
				
			

a) To convert the ‘Marks’ column into numeric values wherever possible.
b) To check whether each element in the ‘Marks’ column is a digit or not.
c) To remove rows from the DataFrame where the ‘Marks’ column contains non-numeric values.
d) To filter rows in the DataFrame where the ‘Marks’ column is numeric)

Correct answer is: b) To check whether each element in the ‘Marks’ column is a digit or not.
Explanation: The code creates a DataFrame `df` with a ‘Marks’ column containing a mixture of strings and numbers. The ‘Numeric’ column is then added to the DataFrame using the `map()` function and a lambda function. The `map()` function applies the lambda function to each element in the ‘Marks’ column, and the lambda function `lambda x: x.isdigit()` checks whether each element is a digit or not. The result of this check, i.e., True (if the element is a digit) or False (if it’s not), is stored in the ‘Numeric’ column.

18). What is the purpose of the following code?

				
					import pandas as pd
df = pd.DataFrame({'Sales': [55000, 75000, 330000, 10000]})
print("Original DataFrame:")
print("Length of sale_amount:")
df['Length'] = df['Sales'].map(str).apply(len)
print(df)
				
			

a) To calculate the total sales amount for each entry in the DataFrame.
b) To calculate the sum of all sales amounts in the DataFrame.
c) To convert the ‘Sales’ column values to strings and find the length of each value.
d) To sort the DataFrame in ascending order based on the ‘Sales’ column.

Correct answer is: c) To convert the ‘Sales’ column values to strings and find the length of each value.
Explanation: In the given code, a DataFrame `df` is created with a single column ‘Sales’ containing four entries representing sales amounts. The code then adds a new column ‘Length’ to the DataFrame by converting the values in the ‘Sales’ column to strings using `map(str)` and then applying the `len` function to calculate the length of each string value.

19). What is the purpose of the following code?

				
					import pandas as pd
import re

d = {'Company_mail': ['TCS tcs@yahoo.com', 'Apple apple@icloud)com', 'Google google@gmail.com']}
df = pd.DataFrame(d)
def find_email(text):
    email = re.findall(r'[\w\.-]+@[\w\.-]+', str(text))
    return ",".join(email)

df['email'] = df['Company_mail'].apply(lambda x: find_email(x))
print("Extracting email from dataframe columns:")
print(df)
				
			

a) To filter rows containing email addresses in the ‘Company_mail’ column.
b) To replace email addresses in the ‘Company_mail’ column with the word ’email’.
c) To split the ‘Company_mail’ column into two columns, one containing the company names and the other containing the email addresses.
d) To extract and store email addresses from the ‘Company_mail’ column into a new ’email’ column.

Correct answer is: d) To extract and store email addresses from the ‘Company_mail’ column into a new ’email’ column.
Explanation: The purpose of the given code is to extract and store email addresses from the ‘Company_mail’ column of the DataFrame into a new column called ’email’. The code uses a regular expression pattern (`r'[\w\.-]+@[\w\.-]+’`) to find email addresses in the text and the `find_email()` function is applied to each row of the ‘Company_mail’ column using the `apply()` method) The result is a DataFrame with an additional ’email’ column containing the extracted email addresses. The `print(df)` statement at the end displays the updated DataFrame with the extracted email addresses.

20). What is the purpose of the following code?

				
					import pandas as pd
df1 = pd.DataFrame({'ID': [1, 2, 3], 'Name': ['Yash', 'Gaurav', 'Sanket'], 'Age': [30, 27, 28]})
df2 = pd.DataFrame({'ID': [4, 3], 'Name': ['Tanmay', 'Athrva'], 'Age': [26, 22]})
result = pd.concat([df1, df2])
print("New dataframe")
print(result)
				
			

a) To merge two DataFrames df1 and df2 based on their common columns.
b) To concatenate two DataFrames df1 and df2 vertically, stacking one below the other.
c) To concatenate two DataFrames df1 and df2 horizontally, merging them side by side.
d) To create a new DataFrame by transposing the columns and rows of df1 and df2.

Correct answer is: b) To concatenate two DataFrames df1 and df2 vertically, stacking one below the other.
Explanation: The given code uses the `pd.concat()` function from the Pandas library to concatenate two DataFrames, df1 and df2, vertically. This results in a new DataFrame called ‘result’ where the rows of df2 are stacked below the rows of df1. The `pd.concat()` function is used to combine DataFrames, and the default behavior is to concatenate them vertically. This is the reason the new DataFrame ‘result’ contains all the rows from df1 followed by all the rows from df2.

21). What is the purpose of the following code?

				
					import pandas as pd
df1 = pd.DataFrame({'ID':[1,2,3],'Name':['Yash','Gaurav','Sanket'],
                   'Age':[30,27,28]})
df2 = pd.DataFrame({'ID':[4,3],'Name':['Tanmay','Athrva'],'Age':[26,22]})
result = pd.concat([df1,df2],axis=1)
print("New dataframe")
print(result)
				
			

a) Merging two DataFrames vertically based on the common column ‘ID’.
b) Merging two DataFrames horizontally based on the common column ‘Name’.
c) Concatenating two DataFrames horizontally, creating a new DataFrame.
d) Concatenating two DataFrames vertically, creating a new DataFrame.

Correct answer is: c) Concatenating two DataFrames horizontally, creating a new DataFrame.
Explanation: The purpose of the given code is to concatenate two DataFrames horizontally using the `pd.concat()` function with `axis=1`. Horizontal concatenation, or concatenation along columns, merges the two DataFrames side by side, based on their column names. The resulting DataFrame `result` will have all columns from both `df1` and `df2` in a single DataFrame. In this case, it will produce a new DataFrame with columns ‘ID’, ‘Name’, and ‘Age’ from both `df1` and `df2`.

22). What is the purpose of the following code?

				
					import pandas as pd
df1 = pd.DataFrame({'Id':['S1','S2','S3'],
                   'Name':['Ketan','Yash','Abhishek'],
                   'Marks':[90,87,77]})
df2 = pd.DataFrame({'Id':['S2','S4'],
                    'Name':['Yash','Gaurav'],
                   'Marks':[70,65]})
print('Dataframe 1: \n',df1)
print('Dataframe 2: \n',df2)
new = pd.merge(df1, df2, on='Id', how='inner')
print("Merged data:")
print(new)
				
			

a) To transpose the rows and columns of the merged DataFrame.
b) To calculate the mean of the ‘Marks’ column in each DataFrame.
c) To merge two DataFrames based on a common column ‘Id’ using an inner join.
d) To merge two DataFrames based on a common column ‘Name’ using an inner join.

Correct answer is: c) To merge two DataFrames based on a common column ‘Id’ using an inner join.
Explanation: The purpose of this code is to merge two DataFrames, `df1` and `df2`, based on a common column ‘Id’ using an inner join. The code uses the `pd.merge()` function, which is a powerful method in Pandas used to combine DataFrames based on shared columns. The ‘inner’ join ensures that only the rows with matching ‘Id’ values in both DataFrames are included in the resulting DataFrame ‘new’. The printed output displays the contents of `df1`, `df2`, and the merged DataFrame ‘new’, allowing us to observe the merging operation.

23). What is the output of the following code?

				
					import pandas as pd
import numpy as np

df = pd.DataFrame({'Sr.no.':[1,2,3,4],
                   'Name':['Alex','John','Peter','Klaus'],
                   'Age':[30,np.nan,29,np.nan]})

print("Original Dataframe: \n",df)
print(df.isna())
				
			

a) Original DataFrame:
Sr.no. Name Age
0 1 Alex 30.0
1 2 John NaN
2 3 Peter 29.0
3 4 Klaus NaN
dtype: float64

Output of `df.isna()`:
Sr.no. Name Age
0 False False False
1 False False True
2 False False False
3 False False True

b) Original DataFrame:
Sr.no. Name Age
0 1 Alex 30.0
1 2 John NaN
2 3 Peter 29.0
3 4 Klaus NaN
dtype: object

Output of `df.isna()`:
Sr.no. Name Age
0 False False False
1 False False True
2 False False False
3 True False True

c) Original DataFrame:
Sr.no. Name Age
0 1 Alex 30.0
1 2 John NaN
2 3 Peter 29.0
3 4 Klaus NaN
dtype: float64

Output of `df.isna()`:
Sr.no. Name Age
0 True False False
1 False False True
2 True False False
3 False False True

d) Original DataFrame:
Sr.no. Name Age
0 1 Alex 30.0
1 2 John NaN
2 3 Peter 29.0
3 4 Klaus NaN
dtype: object

Output of `df.isna()`:
Sr.no. Name Age
0 True False False
1 False False True
2 True False False
3 False False True

Correct answer is: a) Original DataFrame:
Sr.no. Name Age
0 1 Alex 30.0
1 2 John NaN
2 3 Peter 29.0
3 4 Klaus NaN
dtype: float64

Output of `df.isna()`:
Sr.no. Name Age
0 False False False
1 False False True
2 False False False
3 False False True
Explanation: The original DataFrame `df` contains columns ‘Sr.no.’, ‘Name’, and ‘Age’ with corresponding values. The `print(df.isna())` statement prints a DataFrame indicating whether each element in the original DataFrame is null (`NaN`) or not. The output shows `False` for non-null values and `True` for null values in the ‘Age’ column, as indicated by the `NaN` values.

24). What is the purpose of the following code?

				
					import pandas as pd
import numpy as np
df = pd.DataFrame({'Sr.no.':[1,2,3,4,5],
                   'Name':['Alex',np.nan,'Peter','Klaus','Stefan'],
                   'Age':[30,np.nan,29,22,22]})
print("Original Dataframe: \n",df)
result = df.fillna(df.mode().iloc[0])
print(result)
				
			

a) To remove duplicate rows from the DataFrame.
b) To calculate the median of each column in the DataFrame.
c) To fill missing values in the ‘Name’ and ‘Age’ columns with the most frequent value.
d) To calculate the cumulative sum of each column in the DataFrame.

Correct answer is: c) To fill missing values in the ‘Name’ and ‘Age’ columns with the most frequent value.
Explanation: The given code uses the Pandas library to manipulate the DataFrame `df`. The DataFrame contains three columns: ‘Sr.no.’, ‘Name’, and ‘Age’. It uses the `fillna()` method to fill missing values (NaN) in the ‘Name’ and ‘Age’ columns with the most frequent value present in each column. The most frequent value is calculated using the `mode()` function, and `iloc[0]` is used to access the first value in the resulting mode Series. This operation replaces the missing values with the most frequent non-missing value in their respective columns. The filled DataFrame is then stored in the variable `result`, and both the original and filled DataFrames are printed using the `print()` function.

25). What is the purpose of the following code?

				
					import pandas as pd
df = pd.read_excel("file name")
print(df)
				
			

a) To import the Pandas library and read an Excel file named “file name” into a DataFrame, then print the DataFrame.
b) To export an Excel file named “file name” into a Pandas DataFrame, then display the DataFrame.
c) To read a CSV file named “file name” into a Pandas DataFrame, then display the DataFrame.
d) To save a Pandas DataFrame into an Excel file named “file name”, then print the DataFrame.

Correct answer is: a) To import the Pandas library and read an Excel file named “file name” into a DataFrame, then print the DataFrame.
Explanation: The provided code performs the following actions:
1. `import pandas as pd`: This line imports the Pandas library and assigns it the alias `pd` to make it easier to refer to Pandas functions.
2. `df = pd.read_excel(“file name”)`: This line reads an Excel file named “file name” into a Pandas DataFrame `df`. The `read_excel()` function is used to read Excel files and create a DataFrame.
3. `print(df)`: This line prints the DataFrame `df`, which contains the data read from the Excel file.

Python Pandas MCQ : Set 5

Python Pandas MCQ

1). What is the output of the following code?

				
					import pandas as pd
df = pd.Series(['2 Feb 2020','5/11/2021','7-8-2022'])
print("Converting series of date strings to a timeseries:")
print(pd.to_datetime(df))
				
			

a) 0 2020-02-02
1 2021-05-11
dtype: datetime64[ns]

b) 0 Feb 02, 2020
1 May 11, 2021
2 Jul 08, 2022
dtype: datetime64[ns]

c) 0 2020-02-02
1 2021-11-05
2 2022-08-07
dtype: datetime64[ns]

d) ValueError: day is out of range for month

Correct answer is: c) 0 2020-02-02
1 2021-11-05
2 2022-08-07
dtype: datetime64[ns]
Explanation: The given code converts a pandas Series containing date strings into a time series using the `pd.to_datetime()` function. The function interprets various date formats and converts them into datetime64[ns] format. In the original Series, the date strings have different formats: ‘2 Feb 2020’, ‘5/11/2021’, and ‘7-8-2022’. The function `pd.to_datetime()` is capable of handling these formats and correctly converts them into the standard ‘YYYY-MM-DD’ format.

2). What is the output of the following code?

				
					import pandas as pd
df = pd.Series([54, 25, 38, 87, 67])
print("Index of the first smallest and largest value of the series:")
print(df.idxmin())
print(df.idxmax())
				
			

a) Index of the first smallest and largest value of the series:
1
3

b) Index of the first smallest and largest value of the series:
1
4

c) Index of the first smallest and largest value of the series:
2
3

d) Index of the first smallest and largest value of the series:
0
3

Correct answer is: a) Index of the first smallest and largest value of the series:
1
3
Explanation: The code defines a Pandas Series `df` containing five elements: [54, 25, 38, 87, 67]. The `idxmin()` function is then applied to find the index of the first smallest value in the Series, which is 1 (corresponding to the element 25). Similarly, the `idxmax()` function is used to find the index of the first largest value in the Series, which is 3 (corresponding to the element 87).

3). What is the output of the following code?

				
					import pandas as pd
dictionary = {'marks1':[34,20,32,30],'marks2':[36,22,10,44]}
df = pd.DataFrame(dictionary)
print(df)
				
			

a)
marks1 marks2
0 34 36
1 20 22
2 32 10
3 30 44

b)
marks1 marks2
0 34 36
1 20 22
2 32 10
3 30 44
4 22 33

c)
marks1 marks2
0 22 36
1 33 22
2 10 10
3 44 44

d)
marks1 marks2
0 34 22
1 20 30
2 32 32
3 30 10

Correct answer is: a)
marks1 marks2
0 34 36
1 20 22
2 32 10
3 30 44
Explanation: The given code imports the Pandas library as ‘pd’ and creates a DataFrame ‘df’ using a dictionary. The dictionary contains two keys ‘marks1’ and ‘marks2’, each representing a list of four integer values. The DataFrame ‘df’ is printed using the `print()` function. The resulting output displays the DataFrame ‘df’ with the ‘marks1’ and ‘marks2’ columns, and their respective integer values in each row. The values are displayed in the same order as in the dictionary.

4). What is the output of the following code?

				
					import pandas as pd
dictionary = {'marks1': [34, 20, 32, 30], 'marks2': [36, 22, 10, 44]}
df = pd.DataFrame(dictionary)
print("First n rows: \n", df.head(2))
				
			

a)
First n rows:
marks1 marks2
0 34 36
1 20 22

b)
First n rows:
marks1 marks2
0 34 36
1 20 22
2 32 10

c)
First n rows:
marks1 marks2
0 34 36

d)
First n rows:
marks1 marks2
1 20 22
2 32 10

Correct answer is: a)
First n rows:
marks1 marks2
0 34 36
1 20 22
Explanation: The `df.head(2)` function is used to access the first two rows of the DataFrame `df`. The DataFrame `df` is created from the provided dictionary, which has two columns ‘marks1’ and ‘marks2’ with four rows of data each. Therefore, the output will display the first two rows of the DataFrame, as shown in option (a). The head() function displays the specified number of rows from the top of the DataFrame, and since we passed 2 as an argument, it will display the first two rows of the DataFrame.

5). What is the output of the following code?

				
					import pandas as pd
d = {'Sr.no.': [1, 2, 3], 'Name': ['Alex', 'John', 'Peter'], 'Age': [30, 27, 29]}
df = pd.DataFrame(d)
print(df[['Name', 'Age']])
				
			

a)
Name Age
0 Alex 30
1 John 27
2 Peter 29

b)
Name Age
0 [‘Alex’, ‘John’, ‘Peter’] [30, 27, 29]

c)
Name Age
0 Alex 30
2 Peter 29

d)
Sr.no. Name Age
0 1 Alex 30
1 2 John 27

Correct answer is: a)
Name Age
0 Alex 30
1 John 27
2 Peter

Explanation: The provided code creates a DataFrame `df` with three columns: ‘Sr.no.’, ‘Name’, and ‘Age’. The `print(df[[‘Name’, ‘Age’]])` statement selects and prints only the ‘Name’ and ‘Age’ columns from the DataFrame.

6). What is the output of the following code?

				
					import pandas as pd
d = {'Sr.no.':[1,2,3],'Name':['Alex','John','Peter'],'Age':[30,27,29]}
df = pd.DataFrame(d)
print("Second row: \n", df.iloc[1,:])

				
			

a) Sr.no. 2
Name John
Age 27
Name: 1, dtype: object

b) Sr.no. 2
Name Alex
Age 26
Name: 1, dtype: object

c) Sr.no. 2
Name John
Age 29
Name: 1, dtype: object

d) Sr.no. 1
Name John
Age 28
Name: 2, dtype: object

Correct answer is: a) Sr.no. 2
Name John
Age 27
Name: 1, dtype: object
Explanation: The provided code creates a DataFrame ‘df’ with three columns: ‘Sr.no.’, ‘Name’, and ‘Age’, containing the given data) The `iloc` function is used to access rows and columns by their index position. In the `print()` statement, `df.iloc[1, :]` is used to select the second row of the DataFrame.

7). What is the output of the following code?

				
					import pandas as pd
d = {'Sr.no.':[1,2,3,4],'Name':['Alex','John','Peter','Klaus'],'Age':[30,27,29,33]}
df = pd.DataFrame(d)
print("Rows where age is greater than 29")
print(df[df['Age'] > 29])
				
			

a)
Sr.no. Name Age
0 1 Alex 30
3 4 Klaus 33

b)
Sr.no. Name Age
1 2 John 27
2 3 Peter 29

c)
Sr.no. Name Age
3 4 Klaus 33

d)
Sr.no. Name Age
0 1 Alex 30

Correct answer is: a)
Sr.no. Name Age
0 1 Alex 30
3 4 Klaus 33
Explanation: The given code creates a DataFrame ‘df’ with columns ‘Sr.no.’, ‘Name’, and ‘Age’. The `print(df[df[‘Age’] > 29])` statement filters the DataFrame ‘df’ to only include rows where the ‘Age’ column is greater than 29. The resulting output shows the rows where the condition is satisfied) In the original DataFrame ‘df’, there are two rows with ages greater than 29, which are Alex (30) and Klaus (33).

8). Which of the following is the output of the given code?

				
					import pandas as pd
d = {'Sr.no.': [1, 2, 3, 4], 'Name': ['Alex', 'John', 'Peter', 'Klaus'], 'Age': [30, 27, 29, 33]}
df = pd.DataFrame(d)
print("No. of rows: ", df.shape[0])
print("No. of columns: ", df.shape[1])
				
			

a) No. of rows: 3
No. of columns: 4

b) No. of rows: 4
No. of columns: 3

c) No. of rows: 4
No. of columns: 2

d) No. of rows: 2
No. of columns: 4

Correct answer is: b) No. of rows: 4
No. of columns: 3
Explanation: The code creates a DataFrame `df` from the dictionary `d` with 4 rows and 3 columns. The `shape` attribute of the DataFrame is a tuple that contains the number of rows and columns. The statement `df.shape[0]` prints the number of rows, which is 4, and `df.shape[1]` prints the number of columns, which is 3. Therefore, the correct output is “No. of rows: 4” and “No. of columns: 3”.

9). What is the output of the following code?

				
					import pandas as pd
d = {'Sr.no.':[1,2,3,4],'Name':['Alex','John','Peter','Klaus'],'Age':[30,np.nan,29,np.nan]}
df = pd.DataFrame(d)
print("Rows where age is missing:")
print(df[df['Age'].isnull()])
				
			

a)
Sr.no. Name Age
1 2 John NaN
3 4 Klaus NaN

b)
Sr.no. Name Age
0 1 Alex 30.0
2 3 Peter 29.0

c)
Sr.no. Name Age
1 2 John NaN
2 3 Peter NaN

d)
Sr.no. Name Age
1 2 John NaN
3 4 Klaus 29.0

Correct answer is: a)
Sr.no. Name Age
1 2 John NaN
3 4 Klaus NaN
Explanation: The given code creates a DataFrame `df` with four columns: ‘Sr.no.’, ‘Name’, and ‘Age’. Two of the ‘Age’ values are assigned as `np.nan`, which represents missing or not-a-number values in pandas. The code then prints the rows where the ‘Age’ column has missing values using `df[df[‘Age’].isnull()]`.

10). What is the output of the following code?

				
					import pandas as pd
d = {'Sr.no.': [1, 2, 3, 4], 'Name': ['Alex', 'John', 'Peter', 'Klaus'], 'Age': [30, 27, 29, 33]}
df = pd.DataFrame(d)
print("Rows where age is between 25 and 30 (inclusive):")
print(df[df['Age'].between(25, 30)])
				
			

a)
Sr.no. Name Age
1 2 John 27
2 3 Peter 29

b)
Sr.no. Name Age
0 1 Alex 30
1 2 John 27
2 3 Peter 29

c)
Sr.no. Name Age
2 3 Peter 29

d)
Sr.no. Name Age
0 1 Alex 30
1 2 John 27

Correct answer is: b)
Sr.no. Name Age
0 1 Alex 30
1 2 John 27
2 3 Peter 29
Explanation: The given code creates a DataFrame `df` with four columns: ‘Sr.no.’, ‘Name’, and ‘Age’. It then prints the rows where the ‘Age’ column falls between 25 and 30 (inclusive). The `between()` function in Pandas checks whether each element in the ‘Age’ column is between 25 and 30. When we apply this condition to the DataFrame using `df[‘Age’].between(25, 30)`, it returns a boolean Series with `True` for rows where the ‘Age’ is between 25 and 30 and `False` otherwise. The final output displays the rows where the condition is `True`, which corresponds to rows with ‘Age’ values of 27, 29, and 30 (inclusive).

11). What is the output of the following code?

				
					import pandas as pd
d = {'Sr.no.':[1,2,3,4],'Name':['Alex','John','Peter','Klaus'],'Age':[30,27,29,33]}
df = pd.DataFrame(d)
print(df)
print("Change the age of John to 24:")
df['Age'] = df['Age'].replace(27,24)
print(df)
				
			

a)
Sr.no. Name Age
0 1 Alex 30
1 2 John 24
2 3 Peter 29
3 4 Klaus 33

b)
Sr.no. Name Age
0 1 Alex 30
1 2 John 27
2 3 Peter 29
3 4 Klaus 33
Change the age of John to 24:
Sr.no. Name Age
0 1 Alex 30
1 2 John 24
2 3 Peter 29
3 4 Klaus 33

c)
Sr.no. Name Age
0 1 Alex 30
1 2 John 24
2 3 Peter 29
3 4 Klaus 33
Change the age of John to 27:
Sr.no. Name Age
0 1 Alex 30
1 2 John 27
2 3 Peter 29
3 4 Klaus 33

d)
Sr.no. Name Age
0 1 Alex 30
1 2 John 27
2 3 Peter 29
3 4 Klaus 33
Change the age of John to 24:
Sr.no. Name Age
0 1 Alex 30
1 2 John 24
2 3 Peter 29

Correct answer is: b)
Sr.no. Name Age
0 1 Alex 30
1 2 John 27
2 3 Peter 29
3 4 Klaus 33
Change the age of John to 24:
Sr.no. Name Age
0 1 Alex 30
1 2 John 24
2 3 Peter 29
3 4 Klaus 33
Explanation: The code creates a DataFrame named `df` with columns ‘Sr.no.’, ‘Name’, and ‘Age’. The initial DataFrame is as follows:
Sr.no. Name Age
0 1 Alex 30
1 2 John 27
2 3 Peter 29
3 4 Klaus 33
Then, it modifies the ‘Age’ of John by replacing the value 27 with 24.

12). What is the output of the following code?

				
					import pandas as pd
d = {'Sr.no.':[1,2,3,4],'Name':['Alex','John','Peter','Klaus'],'Age':[30,27,29,33]}
df = pd.DataFrame(d)
print("Sum of age columns: ",df['Age'].sum())
				
			

b) Sum of age columns: 119.0
c) Sum of age columns: 119.00
d) Sum of age columns: 120

Correct answer is: a) Sum of age columns: 119
Explanation: The given code creates a DataFrame `df` using the dictionary `d`, which contains three columns: ‘Sr.no.’, ‘Name’, and ‘Age’. Then, it calculates the sum of the ‘Age’ column using the `sum()` function and prints the result. The ‘Age’ column in the DataFrame contains the values [30, 27, 29, 33]. When you calculate the sum of these values, you get 30 + 27 + 29 + 33 = 119.

13). What is the output of the following code?

				
					import pandas as pd
d = {'Sr.no.':[1,2,3,4],'Name':['Alex','John','Peter','Klaus'],'Age':[30,27,29,33]}
df = pd.DataFrame(d)
df1 = {'Sr.no.':5,'Name':'Jason','Age':28}
df = df.append(df1, ignore_index=True)
print("New Series: ")
print(df)
				
			

a)
Sr.no. Name Age
0 1 Alex 30
1 2 John 27
2 3 Peter 29
3 4 Klaus 33
4 5 Jason 28

b)
Sr.no. Name Age
0 1 Alex 30
1 2 John 27
2 3 Peter 29
3 4 Klaus 33
4 Sr.no. Name Age
5 5 Jason 28

c)
Sr.no. Name Age
0 1 Alex 30
1 2 John 27
2 3 Peter 29
3 4 Klaus 33

d)
Sr.no. Name Age
0 1 Alex 30
1 2 John 27
2 3 Peter 29
3 4 Klaus 33
0 5 Jason 28

Correct answer is: a)
Sr.no. Name Age
0 1 Alex 30
1 2 John 27
2 3 Peter 29
3 4 Klaus 33
4 5 Jason 28
Explanation: The code creates a DataFrame `df` with columns ‘Sr.no.’, ‘Name’, and ‘Age’ using a dictionary `d`. Then, it appends a new row represented by the dictionary `df1` to the DataFrame using the `append()` method with `ignore_index=True`. The `ignore_index=True` ensures that the new row is appended with a new index.

14). What is the output of the following code?

				
					import pandas as pd
d = {'Sr.no.': [1, 2, 3, 4], 'Name': ['Alex', 'John', 'Peter', 'Klaus'], 'Age': [30, 27, 29, 33]}
df = pd.DataFrame(d)
new = df.sort_values(by=['Name'], ascending=[True])
print("After sorting:")
print(new)

				
			

a)
After sorting:
Sr.no. Name Age
0 1 Alex 30
1 2 John 27
3 4 Klaus 33
2 3 Peter 29

b)
After sorting:
Sr.no. Name Age
2 3 Peter 29
3 4 Klaus 33
0 1 Alex 30
1 2 John 27

c)
After sorting:
Sr.no. Name Age
3 4 Klaus 33
2 3 Peter 29
1 2 John 27
0 1 Alex 30

d)
After sorting:
Sr.no. Name Age
1 2 John 27
0 1 Alex 30
2 3 Peter 29
3 4 Klaus 33

Correct answer is: a)
After sorting:
Sr.no. Name Age
0 1 Alex 30
1 2 John 27
3 4 Klaus 33
2 3 Peter 29
Explanation: The given code snippet creates a DataFrame `df` with columns ‘Sr.no.’, ‘Name’, and ‘Age’. It then sorts the DataFrame `df` based on the ‘Name’ column in ascending order using the `sort_values()` method with `ascending=True`. The sorted DataFrame is stored in a new variable `new`.

15). What is the output of the following code?

				
					import pandas as pd
d = {'Sr.no.': [1, 2, 3, 4], 'Name': ['Alex', 'John', 'Peter', 'Klaus'], 'Age': [30, 27, 29, 33]}
df = pd.DataFrame(d)
print("Sum of age columns: ", df['Age'].mean())
				
			

a) 30.25
b) 29.75
c) 29.0
d) 32.25

Correct answer is: b) 29.75
Explanation: The given code creates a DataFrame `df` with columns ‘Sr.no.’, ‘Name’, and ‘Age’. It then calculates the mean (average) of the ‘Age’ column using the `mean()` function and prints the result. The ‘Age’ column contains values [30, 27, 29, 33]. To find the mean, you add all the values and divide the sum by the total number of values: Mean = (30 + 27 + 29 + 33) / 4 = 29.75

16). What is the output of the following code?

				
					import pandas as pd
d = {'Sr.no.': [1, 2, 3, 4], 'Name': ['Alex', 'John', 'Peter', 'Klaus'], 'Age': [30, 27, 29, 33]}
df = pd.DataFrame(d)
print("Change the name of John to Jim:")
df['Name'] = df['Name'].replace('John', 'Jim')
print(df)
				
			

a)
Sr.no. Name Age
0 1 Alex 30
2 3 Peter 29
3 4 Klaus 33

b)
Sr.no. Name Age
0 1 Alex 30
1 2 John 27
2 3 Peter 29
3 4 Klaus 33

c)
Sr.no. Name Age
0 1 Alex 30
1 2 Jim 27
2 3 Peter 29
3 4 Klaus 33

d)
Sr.no. Name Age
0 1 Alex 30
1 2 John 27
2 3 Peter 29
3 4 Klaus 33

Correct answer is: c)
Sr.no. Name Age
0 1 Alex 30
1 2 Jim 27
2 3 Peter 29
3 4 Klaus 33
Explanation: The given code creates a DataFrame `df` with columns ‘Sr.no.’, ‘Name’, and ‘Age’. Then, it replaces the value ‘John’ in the ‘Name’ column with ‘Jim’.

17). What is the output of the following code?

				
					import pandas as pd
d = {'Sr.no.':[1,2,3,4],'Name':['Alex','John','Peter','Klaus'],'Age':[30,27,29,33]}
df = pd.DataFrame(d)
df = df[df.Name != 'John']
print("New Series")
print(df)
				
			

a)
New Series
Sr.no. Name Age
0 1 Alex 30
2 3 Peter 29
3 4 Klaus 33

b)
New Series
Sr.no. Name Age
0 1 Alex 30
1 2 John 27
2 3 Peter 29
3 4 Klaus 33

c)
New Series
Sr.no. Name Age
1 2 John 27
2 3 Peter 29
3 4 Klaus 33

d)
New Series
Sr.no. Name Age
0 1 Alex 30
2 3 Peter 29
3 4 Klaus 33
1 2 John 27

Correct answer is: a)
New Series
Sr.no. Name Age
0 1 Alex 30
2 3 Peter 29
3 4 Klaus 33
Explanation: The code creates a DataFrame `df` with three columns: ‘Sr.no.’, ‘Name’, and ‘Age’. Then, it filters the DataFrame using the condition `df.Name != ‘John’`, which keeps all rows where the ‘Name’ column is not equal to ‘John’. The resulting DataFrame is printed as “New Series.”

18). What is the output of the following code?

				
					import pandas as pd
d = {'Sr.no.': [1, 2, 3, 4], 'Name': ['Alex', 'John', 'Peter', 'Klaus'], 'Age': [30, 27, 29, 33]}
df = pd.DataFrame(d)
Salary = [50000, 65000, 58000, 66000]
df['Salary'] = Salary
print("New Series: \n", df)
				
			

a)
Sr.no. Name Age Salary
0 1 Alex 30 50000
1 2 John 27 65000
2 3 Peter 29 58000
3 4 Klaus 33 66000

b)
Sr.no. Name Age
0 1 Alex 30
1 2 John 27
2 3 Peter 29
3 4 Klaus 33

c)
Sr.no. Name Age Salary
0 1 Alex 30 58000
1 2 John 27 65000
2 3 Peter 29 66000
3 4 Klaus 33 50000

d)
Sr.no. Name Age Salary
0 1 Alex 30 66000
1 2 John 27 65000
2 3 Peter 29 50000
3 4 Klaus 33 58000

Correct answer is: a)
Sr.no. Name Age Salary
0 1 Alex 30 50000
1 2 John 27 65000
2 3 Peter 29 58000
3 4 Klaus 33 66000
Explanation: The given code first creates a dictionary `d` containing data for the ‘Sr.no.’, ‘Name’, and ‘Age’ columns. It then creates a DataFrame `df` using this dictionary. Next, it defines a list `Salary` containing salary values and adds it to the DataFrame as a new column called ‘Salary’. The output of the `print()` function will display the updated DataFrame with the ‘Sr.no.’, ‘Name’, ‘Age’, and ‘Salary’ columns.

19). What is the output of the following code?

				
					import pandas as pd
d = [{'name':'Yash','percentage':78},{'name':'Rakesh','percentage':80},{'name':'Suresh','percentage':60}]
df = pd.DataFrame(d)
for index, row in df.iterrows():
    print(row['name'], row['percentage'])
				
			

a) Yash 78 Rakesh 80 Suresh 60
b) Yash Rakesh Suresh
c) 0 Yash 1 Rakesh 2 Suresh
d) Yash percentage Rakesh percentage Suresh percentage

Correct answer is: a) Yash 78 Rakesh 80 Suresh 60
Explanation: The code creates a DataFrame `df` using a list of dictionaries `d`. The DataFrame contains three rows, with ‘name’ and ‘percentage’ as columns. The `iterrows()` method is used to iterate over the rows of the DataFrame, and for each row, the ‘name’ and ‘percentage’ values are printed)

20). What is the output of the following code?

				
					import pandas as pd
d = {'name': ['Virat', 'Messi', 'Kobe'], 'sport': ['cricket', 'football', 'basketball']}
df = pd.DataFrame(d)
print("Names of columns: ")
print(list(df.columns.values))
				
			

a) `[‘name’, ‘sport’]`
b) `[‘Virat’, ‘Messi’, ‘Kobe’, ‘cricket’, ‘football’, ‘basketball’]`
c) `[‘name’, ‘Virat’, ‘Messi’, ‘Kobe’, ‘sport’, ‘cricket’, ‘football’, ‘basketball’]`
d) `[‘cricket’, ‘football’, ‘basketball’, ‘name’, ‘sport’]`

Correct answer is: a) `[‘name’, ‘sport’]`
Explanation: The given code first imports the pandas library and creates a DataFrame `df` from the dictionary `d`. The dictionary `d` consists of two key-value pairs: `’name’` with a list of names and `’sport’` with a list of corresponding sports.

21). What is the output of the following code?

				
					import pandas as pd
d = {'C1': [1, 3, 8], 'C2': [6, 8, 0], 'C3': [8, 2, 6]}
df = pd.DataFrame(d)
df = df.rename(columns={'C1': 'A', 'C2': 'B', 'C3': 'C'})
print("New DataFrame after renaming columns:")
print(df)
				
			

a)
New DataFrame after renaming columns:
A B C
0 1 6 8
1 3 8 2
2 8 0 6

b)
New DataFrame after renaming columns:
C1 C2 C3
0 1 6 8
1 3 8 2
2 8 0 6

c)
New DataFrame after renaming columns:
C1 C2 C3
0 1 3 8
1 6 8 0
2 8 2 6

d)
New DataFrame after renaming columns:
A B C
0 1 3 8
1 6 8 0
2 8 2 6

Correct answer is: a)
New DataFrame after renaming columns:
A B C
0 1 6 8
1 3 8 2
2 8 0 6
Explanation: The code creates a DataFrame `df` using the dictionary `d`, and then it renames the columns ‘C1’, ‘C2’, and ‘C3’ to ‘A’, ‘B’, and ‘C’ respectively. The `print(df)` statement displays the new DataFrame after renaming the columns. The output shows that the DataFrame `df` has been updated with the new column names ‘A’, ‘B’, and ‘C’, while maintaining the original data)

22). What is the output of the following code?

				
					import pandas as pd
d = {'Sr.no.':[1,2,3,4],'Name':['Alex','John','Peter','Klaus'],'Age':[30,27,29,33],'Salary':[50000,65000,58000,66000]}
df = pd.DataFrame(d)
df = df[['Sr.no.','Name','Salary','Age']]
print('After re-ordering columns: \n',df)
				
			

a) After re-ordering columns:
Sr.no. Name Salary Age
0 1 Alex 50000 30
1 2 John 65000 27
2 3 Peter 58000 29
3 4 Klaus 66000 33

b) After re-ordering columns:
Sr.no. Name Age Salary
0 1 Alex 30 50000
1 2 John 27 65000
2 3 Peter 29 58000
3 4 Klaus 33 66000

c) After re-ordering columns:
Sr.no. Salary Age Name
0 1 50000 30 Alex
1 2 65000 27 John
2 3 58000 29 Peter
3 4 66000 33 Klaus

d) The code will raise an error.

Correct answer is: a) After re-ordering columns:
Sr.no. Name Salary Age
0 1 Alex 50000 30
1 2 John 65000 27
2 3 Peter 58000 29
3 4 Klaus 66000 33
Explanation: The code first creates a DataFrame `df` using a dictionary `d`, which contains ‘Sr.no.’, ‘Name’, ‘Age’, and ‘Salary’ as keys, and their respective values. The DataFrame is then re-ordered to have ‘Sr.no.’, ‘Name’, ‘Salary’, and ‘Age’ as columns using the line `df = df[[‘Sr.no.’,’Name’,’Salary’,’Age’]]`. Finally, the output is printed using `print(‘After re-ordering columns: \n’, df)`.

23). What is the purpose of the following code?

				
					import pandas as pd
d = {'Sr.no.': [1, 2, 3, 4], 'Name': ['Alex', 'John', 'Peter', 'Klaus'], 'Age': [30, 27, 29, 33], 'Salary': [50000, 65000, 58000, 66000]}
df = pd.DataFrame(d)
df.to_csv('new_file.csv', sep='\t', index=False)
new = pd.read_csv('new_file.csv')
print(new)

				
			

a) To read a CSV file into a DataFrame and display its contents.
b) To create a new CSV file and write the DataFrame contents into it.
c) To convert the DataFrame into a NumPy array and print its values.
d) To sort the DataFrame based on the ‘Age’ column and print the result.

Correct answer is: b) To create a new CSV file and write the DataFrame contents into it.
Explanation: The given code performs the following operations:
1. A dictionary `d` is defined containing data related to ‘Sr.no.’, ‘Name’, ‘Age’, and ‘Salary’.
2. A DataFrame `df` is created using the data from the dictionary `d`.
3. The `to_csv()` method is used to export the DataFrame `df` to a CSV file named ‘new_file.csv’, using tab (`\t`) as the separator and excluding the index column.
4. The `pd.read_csv()` function is used to read the contents of the ‘new_file.csv’ CSV file back into a new DataFrame `new`.
5. Finally, the contents of the DataFrame `new` are printed, displaying the data from the ‘new_file.csv’ file.

24). What is the purpose of the following code?

				
					import pandas as pd
df = pd.read_csv("population_by_country_2020.csv")
df1 = df.drop(['Yearly Change','Net Change','Density (P/Km²)','Land Area (Km²)',
              'Migrants (net)','Fert. Rate','Med) Age','Urban Pop %','World Share'],axis=1)
print(df1.head(10))
				
			

a) To read a CSV file named “population_by_country_2020.csv” and display the first 10 rows of the DataFrame.
b) To drop specific columns from the DataFrame ‘df’ and display the first 10 rows of the modified DataFrame.
c) To filter the DataFrame ‘df’ and display the first 10 rows that meet a certain condition.
d) To rename specific columns in the DataFrame ‘df’ and display the first 10 rows of the modified DataFrame.

Correct answer is: b) To drop specific columns from the DataFrame ‘df’ and display the first 10 rows of the modified DataFrame.
Explanation: The given code imports the Pandas library as ‘pd’ and then reads a CSV file named “population_by_country_2020.csv” into a DataFrame ‘df’ using the `pd.read_csv()` function. After that, the code creates a new DataFrame ‘df1’ by dropping specific columns from ‘df’ using the `drop()` method) The columns [‘Yearly Change’, ‘Net Change’, ‘Density (P/Km²)’, ‘Land Area (Km²)’, ‘Migrants (net)’, ‘Fert. Rate’, ‘Med) Age’, ‘Urban Pop %’, ‘World Share’] are dropped from the DataFrame ‘df’. Finally, the code prints the first 10 rows of the modified DataFrame ‘df1’ using the `head(10)` method)

25). What is the purpose of the following code?

				
					import pandas as pd
d = {'Sr.no.':[1,2,3,4],'Name':['Alex','John','Peter','Klaus'],'Age':[30,np.nan,29,np.nan]}
df = pd.DataFrame(d)
print(df)
df.fillna(value = 25,inplace = True)
print("After filling nan values: \n",df)
				
			

a) To calculate the mean of the ‘Age’ column and fill the missing values with the mean.
b) To replace all the missing values in the ‘Age’ column with the value 25.
c) To drop the rows with missing values in the ‘Age’ column from the DataFrame.
d) To calculate the median of the ‘Age’ column and fill the missing values with the median.

Correct answer is: b) To replace all the missing values in the ‘Age’ column with the value 25.
Explanation: The given code is using the Pandas library to create a DataFrame `df` with columns ‘Sr.no.’, ‘Name’, and ‘Age’. The ‘Age’ column contains NaN (Not a Number) values. The `fillna()` method is then used to fill the missing (NaN) values in the ‘Age’ column with the value 25. The `inplace=True` argument is used to modify the DataFrame `df` in place, meaning the changes are applied directly to the original DataFrame.

Python Pandas MCQ : Set 4

Python Pandas MCQ

1). What is the output of the following code?

				
					import pandas as pd
df = pd.Series([15, 43, 88, 23])
print(df)
				
			

a) 15, 43, 88, 23
b) [15, 43, 88, 23]
c) 0 15
1 43
2 88
3 23
dtype: int64
d) None of the above

Correct answer is: c) 0 15
1 43
2 88
3 23
dtype: int64
Explanation: The code creates a Pandas Series named `df` with values [15, 43, 88, 23]. When the `print(df)` statement is executed, it prints the Series object `df` along with the index and the data type. In this case, the output displays the index values (0, 1, 2, 3) and the corresponding data values (15, 43, 88, 23) with a dtype of int64.

2). What is the output of the following code?

				
					import pandas as pd
df = pd.Series([15, 43, 88, 23])
print(df)
print(type(df))
print("Convert Pandas Series to Python list")
print(df.tolist())
print(type(df.tolist()))
				
			

a)
0 15
1 43
2 88
3 23
dtype: int64
<class ‘pandas.core.series.Series’>
Convert Pandas Series to Python list
[15, 43, 88, 23]
<class ‘list’>

b) 0 15
1 43
2 88
3 23
dtype: int32
<class ‘pandas.core.series.Series’>
Convert Pandas Series to Python list
[15, 43, 88, 23]
<class ‘list’>

c) 15 43 88 23
<class ‘pandas.core.series.Series’>
Convert Pandas Series to Python list
[15, 43, 88, 23]
<class ‘list’>

d) 15 43 88 23
<class ‘pandas.core.series.Series’>
Convert Pandas Series to Python list
15 43 88 23
<class ‘list’>

Correct answer is: a)
0 15
1 43
2 88
3 23
dtype: int64
<class ‘pandas.core.series.Series’>
Convert Pandas Series to Python list
[15, 43, 88, 23]
<class ‘list’>
Explanation: The code first creates a Pandas Series object named `df` with the values `[15, 43, 88, 23]`. When printing `df`, it displays the Series with the index and values. Next, the code prints the type of `df`, which is `<class ‘pandas.core.series.Series’>`.After that, it prints “Convert Pandas Series to Python list” as a message. Following that, `df.tolist()` converts the Pandas Series `df` into a Python list. When printing `df.tolist()`, it displays the list of values `[15, 43, 88, 23]`.

3). What is the output of the following code?

				
					import pandas as pd
df1 = pd.Series([1,6,9,5])
df2 = pd.Series([5,2,4,5])
print(df1+df2)
				
			

a) [6, 8, 13, 10]
b) [1, 6, 9, 5, 5, 2, 4, 5]
c) [6, 8, 13, 10, NaN, NaN, NaN, NaN]
d) [1, 6, 9, 5, 5, 2, 4, 5, NaN, NaN, NaN, NaN]

Correct answer is: a) [6, 8, 13, 10]
Explanation: The code creates two Series, `df1` and `df2`, with the values [1, 6, 9, 5] and [5, 2, 4, 5] respectively. When we perform the addition operation `df1+df2`, Pandas aligns the Series based on their indices and performs element-wise addition. In this case, the resulting Series will be [1+5, 6+2, 9+4, 5+5], which simplifies to [6, 8, 13, 10]. Therefore, the output of the code will be [6, 8, 13, 10].

4). What is the output of the following code?

				
					import pandas as pd
df1 = pd.Series([1, 6, 9, 5])
df2 = pd.Series([5, 2, 4, 5])
print(df1 - df2)
				
			

a) [4, 4, 5, 0]
b) [6, 4, 5, 0]
c) [1, 4, 5, 0]
d) [4, 2, 5, 0]

Correct answer is: a) [4, 4, 5, 0]
Explanation: When subtracting two Series in Pandas, the operation is performed element-wise. In this case, `df1 – df2` subtracts the corresponding elements of `df2` from `df1`. Thus, the resulting Series is [4, 4, 5, 0].

5). What is the output of the following code?

				
					import pandas as pd
df1 = pd.Series([1, 6, 9, 5])
df2 = pd.Series([5, 2, 4, 5])
print(df1 * df2)
				
			

a) 5, 12, 36, 25
b) 5, 12, 36, 30
c) 5, 12, 36, 45
d) 5, 6, 36, 25

Correct answer is: c) 5, 12, 36, 45
Explanation: The code creates two pandas Series objects, `df1` and `df2`, with corresponding values [1, 6, 9, 5] and [5, 2, 4, 5]. When the multiplication operator `*` is applied to these two Series, element-wise multiplication is performed) Therefore, the resulting Series will contain the products of the corresponding elements from `df1` and `df2`.

6). What is the output of the following code?

				
					import pandas as pd
df1 = pd.Series([1,6,9,5])
df2 = pd.Series([5,2,4,5])
print(df1/df2)
				
			

a) 0 0.2
1 3.0
2 2.25
3 1.0
dtype: float64

b) 0 0.2
1 3.0
2 2.25
3 5.0
dtype: float64

c) 0 5.0
1 3.0
2 2.25
3 5.0
dtype: float64

d) 0 0.2
1 6.0
2 2.25
3 1.0
dtype: float64

Correct answer is: a
Explanation: When dividing two Series (`df1` and `df2`), Pandas perform element-wise division. The resulting Series will have the same indices as the original Series.

7). What is the output of the following code?

				
					import pandas as pd
df1 = pd.Series([1, 6, 9, 5])
df2 = pd.Series([5, 2, 4, 5])
print("Equals:")
print(df1 == df2)
				
			

a) Equals:
0 False
1 False
2 False
3 True
dtype: bool

b) Equals:
0 False
1 True
2 False
3 True
dtype: bool

c) Equals:
0 True
1 False
2 False
3 True
dtype: bool

d) Equals:
0 False
1 False
2 True
3 False
dtype: bool

Correct answer is: a) Equals:
0 False
1 False
2 False
3 True
dtype: bool
Explanation: The code compares each element of `df1` with the corresponding element of `df2` and returns a boolean Series indicating whether each pair of elements is equal or not. In this case, the output shows that the first three pairs are not equal (`False`), while the last pair is equal (`True`).

8). What is the output of the following code?

				
					import pandas as pd
df1 = pd.Series([1, 6, 9, 5])
df2 = pd.Series([5, 2, 4, 5])
print("Greater than:")
print(df1 > df2)
				
			

a) Greater than:
0 False
1 True
2 True
3 False
dtype: bool

b) Greater than:
0 True
1 False
2 True
3 False
dtype: bool

c) Greater than:
0 False
1 False
2 True
3 False
dtype: bool

d) Greater than:
0 True
1 True
2 False
3 False
dtype: bool

Correct answer is: a) Greater than:
0 False
1 True
2 True
3 False
dtype: bool
Explanation: The code compares the elements of `df1` and `df2` element-wise using the `>` operator. In the output, each element represents the result of the comparison between the corresponding elements of `df1` and `df2`. If an element in `df1` is greater than the corresponding element in `df2`, the result will be `True`, otherwise `False`. In this case, the first element is not greater (False), the second and third elements are greater (True), and the fourth element is not greater (False).

9). What is the output of the following code?

				
					import pandas as pd
D = {'name':'Virat','sport':'cricket','age':32}
result = pd.Series(D)
print(result)
				
			

a)
name Virat
sport cricket
age 32
dtype: object

b)
0 Virat
1 cricket
2 32
dtype: object

c) {‘name’:’Virat’,’sport’:’cricket’,’age’:32}

d)
name cricket
sport Virat
age 32
dtype: object

Correct answer is: a)
name Virat
sport cricket
age 32
dtype: object
Explanation: The code creates a dictionary `D` with three key-value pairs representing a person’s name, sport, and age. It then converts the dictionary into a Pandas Series using `pd.Series(D)`. The resulting Series, `result`, will have the keys of the dictionary as the index labels and the corresponding values as the data)

10). What is the output of the following code?

				
					import numpy as np
import pandas as pd

array = np.array([5, 3, 8, 9, 0])
result = pd.Series(array)
print(result)

				
			

a) 5 3 8 9 0
b) 0 9 8 3 5
c) [5, 3, 8, 9, 0]
d) It will raise an error.

Correct answer is: c) [5, 3, 8, 9, 0]
Explanation: The code creates a NumPy array `array` with values [5, 3, 8, 9, 0]. Then, it creates a Pandas Series `result` from the array. Finally, it prints the Series `result`. The output of the code is a Pandas Series object represented as [5, 3, 8, 9, 0]. The values from the NumPy array are preserved in the Series.

11). What is the output of the following code?

				
					import pandas as pd
df1 = pd.Series([54, 27.90, 'sqa', 33.33, 'tools'])
print("Original Data Series:")
print(df1)
print("Change the said data type to numeric:")
df2 = pd.to_numeric(df1, errors='coerce')
print(df2)
				
			

a) Original Data Series:
0 54
1 27.9
2 sqa
3 33.33
4 tools
dtype: object
Change the said data type to numeric:
0 54.00
1 27.90
2 NaN
3 33.33
4 NaN
dtype: float64

b) Original Data Series:
0 54
1 27.9
2 sqa
3 33.33
4 tools
dtype: object
Change the said data type to numeric:
0 54
1 27.9
2 NaN
3 33.33
4 NaN
dtype: float64

c) Original Data Series:
0 54
1 27.9
2 sqa
3 33.33
4 tools
dtype: object
Change the said data type to numeric:
0 54.00
1 27.90
2 sqa
3 33.33
4 tools
dtype: object

d) Original Data Series:
0 54
1 27.9
2 sqa
3 33.33
4 tools
dtype: object
Change the said data type to numeric:
0 54
1 27.9
2 sqa
3 33.33
4 tools
dtype: object

Correct answer is: a) Original Data Series:
0 54
1 27.9
2 sqa
3 33.33
4 tools
dtype: object
Change the said data type to numeric:
0 54.00
1 27.90
2 NaN
3 33.33
4 NaN
dtype: float64
Explanation: The code first creates a Series `df1` with a mix of integer, float, and string values. Then it prints the original data series. After that, it uses the `pd.to_numeric()` function to convert the data type of `df1` to numeric) The `errors=’coerce’` parameter is used to convert non-numeric values to NaN (Not a Number) instead of raising an error. The resulting converted Series is stored in `df2`. Finally, it prints the converted Series `df2`.

12). What is the output of the following code?

				
					import numpy as np
import pandas as pd

df1 = pd.Series([54, 27.90, 'sqa', 33.33, 'tools'])
print("Original Data Series:")
print(df1)
print("Series to an array")
result = np.array(df1.values.tolist())
print(result)
				
			

a) [54, 27.9, ‘sqa’, 33.33, ‘tools’]
d) [54, 27.9, 33.33]
c) [’54’, ‘27.9’, ‘sqa’, ‘33.33’, ‘tools’]
d) Error: cannot convert ‘sqa’ and ‘tools’ to float

Correct answer is: a) [54, 27.9, ‘sqa’, 33.33, ‘tools’]
Explanation: The code creates a Pandas Series (`df1`) with values [54, 27.90, ‘sqa’, 33.33, ‘tools’]. It then converts the Series into a NumPy array using `np.array(df1.values.tolist())`. The resulting array contains all the values from the Series, including the numeric values and the string values. Therefore, the output of the code is [54, 27.9, ‘sqa’, 33.33, ‘tools’].

13). What is the output of the following code?

				
					import pandas as pd
a = [['Sqa', 'tool'], ['Learning', 'python'], ['Is', 'fun']]
result = pd.Series(a)
print(result)
				
			

a)
0 [Sqa, tool]
1 [Learning, python]
2 [Is, fun]
dtype: object

d)
0 Sqa tool
1 Learning python
2 Is fun
dtype: object

c)
0 Sqa
1 Learning
2 Is
dtype: object

0 tool
1 python
2 fun
dtype: object

d)
0 Sqa Learning Is
1 tool python fun
dtype: object

Correct answer is: a)
0 [Sqa, tool]
1 [Learning, python]
2 [Is, fun]
dtype: object
Explanation: The code creates a Pandas Series object `result` from the list `a`. Each element in the list becomes a separate entry in the Series. Since each element of `a` is a list itself, the output shows the list elements as individual entries in the Series, resulting in a Series with three rows and two columns. The output displays the Series with the index labels and the corresponding values.

14). What is the output of the following code?

				
					import pandas as pd
df = pd.Series([55, 23, 10, 87])
print("Original Data Series:")
print(df)
new = pd.Series(df).sort_values()
print(new)
				
			

a) Original Data Series:
0 55
1 23
2 10
3 87
dtype: int64
2 10
1 23
0 87
3 55
dtype: int64

b) Original Data Series:
55
23
10
87
dtype: int64
10
23
55
87
dtype: int64

c) Original Data Series:
0 55
1 23
2 10
3 87
dtype: int64
3 87
2 10
1 23
0 55
dtype: int64

d) Original Data Series:
55
23
10
87
dtype: int64
87
10
55
23
dtype: int64

Correct answer is: b) Original Data Series:
55
23
10
87
dtype: int64
10
23
55
87
dtype: int64
Explanation: The code first creates a Series `df` with values [55, 23, 10, 87]. It then prints the original data series. Next, it creates a new Series `new` by sorting the values of `df` in ascending order.

15). What is the output of the following code?

				
					import pandas as pd
df = pd.Series([54,27.9,'sqa',33.33,'tools'])
print("Original Data Series:")
print(df)
print("Data Series after adding some data:")
new = df.append(pd.Series(['python',100]))
print(new)
				
			

0 54
1 27.9
2 sqa
3 33.33
4 tools
dtype: object

Data Series after adding some data:
0 54
1 27.9
2 sqa
3 33.33
4 tools
5 python
6 100
dtype: object

b) Original Data Series:
0 54
1 27.9
2 sqa
3 33.33
4 tools
dtype: object

Data Series after adding some data:
0 54
1 27.9
2 sqa
3 33.33
4 tools
dtype: object

c) Original Data Series:
0 54
1 27.9
2 sqa
3 33.33
4 tools
dtype: object

Data Series after adding some data:
0 54
1 27.9
2 sqa
3 33.33
4 tools
5 python
dtype: int64

d) Original Data Series:
0 54
1 27.9
2 sqa
3 33.33
4 tools
dtype: object

Data Series after adding some data:
0 54
1 27.9
2 sqa
3 33.33
4 python
5 100
dtype: float64

Correct answer is: a) Original Data Series:
0 54
1 27.9
2 sqa
3 33.33
4 tools
dtype: object
Data Series after adding some data:
0 54
1 27.9
2 sqa
3 33.33
4 tools
5 python
6 100
dtype: object
Explanation: The code initializes a Pandas Series named `df` with values `[54, 27.9, ‘sqa’, 33.33, ‘tools’]`. It then prints the original data series, which displays the values in the series. Next, the code appends a new Pandas Series to the existing `df` series using the `append()` function. The new series contains the values `[‘python’, 100]`. The appended series is assigned to the variable `new`. Finally, the code prints the modified data series `new`, which includes the original values as well as the appended values. The output displays the combined series with values `[54, 27.9, ‘sqa’, 33.33, ‘tools’, ‘python’, 100]`. The dtype is ‘object’ because the series contains mixed data types.

16). What is the output of the following code?

				
					import pandas as pd
df = pd.Series([10, 25, 69, 74, 33, 54, 21])
print(df)
print("Subset of the above Data Series:")
new = df[df < 40]
print(new)
				
			

a)
0 10
1 25
2 69
3 74
4 33
5 54
6 21
dtype: int64
Subset of the above Data Series:
0 10
1 25
4 33
6 21
dtype: int64

b)
10
25
69
74
33
54
21
Subset of the above Data Series:
10
25
33
21

c)
10
25
69
74
33
54
21
Subset of the above Data Series:
10
25
33

d)
0 10
1 25
2 69
3 74
4 33
5 54
6 21
dtype: int64
Subset of the above Data Series:
33
21

Correct answer is: a)
0 10
1 25
2 69
3 74
4 33
5 54
6 21
dtype: int64
Subset of the above Data Series:
0 10
1 25
4 33
6 21
dtype: int64
Explanation: The code begins by importing the Pandas library and creating a Series called `df` with the values [10, 25, 69, 74, 33, 54, 21]. This shows the index and corresponding values of the `df` Series. Next, the statement `print(“Subset of the above Data Series:”)` is executed, which simply prints the given text. Then, a new Series called `new` is created by selecting the elements from `df` where the value is less than 40 (`df[df < 40]`). The resulting `new` Series contains the values [10, 25, 33, 21].

17). What is the output of the following code?

				
					import pandas as pd
df = pd.Series([5, 4, 8, 6])
print(df)
print("Mean of the given series: ", df.mean())
				
			

a)
0 5
1 4
2 8
3 6
dtype: int64
Mean of the given series: 5.75

b)
5 0
4 1
8 2
6 3
dtype: int64
Mean of the given series: 5.75

c)
0 5
1 4
2 8
3 6
dtype: int64
Mean of the given series: 5.5

d)
5 0
4 1
8 2
6 3
dtype: int64
Mean of the given series: 5.5

Correct answer is: a)
0 5
1 4
2 8
3 6
dtype: int64
Mean of the given series: 5.75
Explanation: The code first creates a Pandas Series object `df` with the values [5, 4, 8, 6]. When printed, the output shows the series with index labels and corresponding values. The mean of the series is calculated using the `mean()` function, which returns the average value of the series. In this case, the mean is 5.75.

18). What is the output of the following code?

				
					import pandas as pd
df = pd.Series([2, 4, 6, 8, 10])
print(df)
print("Standard deviation of the given series: ", df.std())

				
			

a)
0 2
1 4
2 6
3 8
4 10
dtype: int64
Standard deviation of the given series: 2.8284271247461903

b)
2 0
4 1
6 2
8 3
10 4
dtype: int64
Standard deviation of the given series: 3.1622776601683795

c)
2 0
4 0
6 0
8 0
10 0
dtype: int64
Standard deviation of the given series: 4.47213595499958

d)
0 2
1 4
2 6
3 8
4 10
dtype: int64
Standard deviation of the given series: 3.1622776601683795

Correct answer is: d)
0 2
1 4
2 6
3 8
4 10
dtype: int64
Standard deviation of the given series: 3.1622776601683795
Explanation: The code creates a Pandas Series `df` with values [2, 4, 6, 8, 10]. When printing `df`, it displays the series with index labels and corresponding values. The `df.std()` calculates the standard deviation of the given series, which is 3.1622776601683795. This value is printed along with the string “Standard deviation of the given series: “.

19). What is the output of the following code?

				
					import pandas as pd
df1 = pd.Series([4, 7, 3, 10])
df2 = pd.Series([9, 4, 3])
print("First series: \n", df1)
print("Second series: \n", df2)
result = df1[~df1.isin(df2)]
print(result)
				
			

a) 7, 10
b) 9, 4, 3
c) 3, 7, 10
d) 4, 7, 3, 10

Correct answer is: a) 7, 10
Explanation: The given code first creates two Series objects, `df1` and `df2`, with values [4, 7, 3, 10] and [9, 4, 3] respectively. Then, it prints the contents of both Series using the `print()` function. Next, the code assigns to the variable `result` the values from `df1` that are not present in `df2` using the `~` operator and the `isin()` method) In this case, `~df1.isin(df2)` checks which values in `df1` are not found in `df2`. The resulting Boolean Series is then used to filter `df1`, storing only the values that are not present in `df2` in the variable `result`. Finally, the code prints the contents of `result`.

20). What is the output of the following code?

				
					import pandas as pd
df = pd.Series([54, 38, 67, 87])
print(df)
print("Maximum value: ", df.max())

				
			

a)
0 54
1 38
2 67
3 87
dtype: int64
Maximum value: 87

b)
54 0
38 1
67 2
87 3
dtype: int64
Maximum value: 87

c)
0 54
1 38
2 67
3 87
dtype: int64
Maximum value: 3

d)
54
Maximum value: 87

Correct answer is: a)
0 54
1 38
2 67
3 87
dtype: int64
Maximum value: 87
Explanation: The given code first creates a Pandas Series object named `df` with values [54, 38, 67, 87]. When the `print(df)` statement is executed, it displays the Series object with the index and corresponding values. This shows the index (0, 1, 2, 3) and the corresponding values (54, 38, 67, 87) in the Series. Next, the `print(“Maximum value: “, df.max())` statement is executed) It calculates the maximum value in the Series using the `max()` function and displays it along with the specified text.

21). What is the output of the following code?

				
					import pandas as pd
df = pd.Series([54, 38, 67, 87])
print("Minimum value: ", df.min())
				
			

a) Minimum value: 54
b) Minimum value: 38
c) Minimum value: 67
d) Minimum value: 87

Correct answer is: b) Minimum value: 38
Explanation: The code creates a Pandas Series called `df` with the values [54, 38, 67, 87]. The `min()` function is then applied to the Series, which returns the minimum value from the Series. In this case, the minimum value is 38. The print statement displays the output as “Minimum value: 38”. Therefore, option b is the correct answer.

22). What is the output of the following code?

				
					import pandas as pd
df = pd.Series([3, 0, 3, 2, 2, 0, 3, 3, 2])
result = df.value_counts()
print(result)
				
			

a) 0 2
2 3
3 4
dtype: int64
b) 0 3
2 3
3 3
dtype: int64
c) 0 2
2 2
3 3
dtype: int64
d) 0 3
2 2
3 4
dtype: int64

Correct answer is: a) 0 2
2 3
3 4
dtype: int64
Explanation: The code creates a Pandas Series `df` with a list of values. The `value_counts()` function is then applied to the Series to count the occurrences of each unique value. The resulting output is a new Series object `result` that contains the counts of each unique value in the original Series. In this case, the unique values in `df` are [3, 0, 2]. The counts of these values are 4 for 3, 2 for 0, and 3 for 2.

23). What is the output of the following code?

				
					import pandas as pd

df = pd.Series([3, 0, 3, 2, 2, 0, 3, 3, 2])
positions = [1, 4, 7]
result = df.take(positions)
print("Extract items at given positions of the said series:")
print(result)
				
			

a) 0, 2, 3
b) 0, 2, 2
c) 0, 2, 3, 3
d) 0, 3, 3

Correct answer is: a) 0, 2, 3
Explanation: In the given code, the `take()` function is used to extract items at the specified positions from the `df` Series. The positions `[1, 4, 7]` correspond to the elements at index 1, 4, and 7 in the Series. Therefore, the output of `print(result)` will be `0, 2, 3`.

24). What is the output of the following code?

				
					import pandas as pd
df = pd.Series(['sqatools', 'python', 'data', 'science'])
result = df.map(lambda x: x[0].upper() + x[1:-1] + x[-1].upper())
print("First and last character of each word to upper case:")
print(result)
				
			

a) `[‘SqaToolS’, ‘PythoN’, ‘DatA’, ‘SciencE’]`
d) `[‘SqatoolS’, ‘Python’, ‘DatA’, ‘SciencE’]`
c) `[‘SqaTools’, ‘PythoN’, ‘Data’, ‘Science’]`
d) `[‘sqatoolS’, ‘PythoN’, ‘Data’, ‘SciencE’]`

Correct answer is: a) `[‘SqaToolS’, ‘PythoN’, ‘DatA’, ‘SciencE’]`
Explanation: The lambda function applied to each element of the Series `df` extracts the first character of each word and converts it to uppercase using `x[0].upper()`. It then appends the middle characters `x[1:-1]` as they are, and finally converts the last character to uppercase using `x[-1].upper()`. The resulting Series `result` contains the modified strings with the first and last character of each word in uppercase. The output of the code will be `[‘SqaToolS’, ‘PythoN’, ‘DatA’, ‘SciencE’]`.

25). What is the output of the following code?

				
					import pandas as pd
df = pd.Series(['virat', 'rohit', 'pant', 'shikhar'])
result = df.map(lambda x: len(x))
print("Number of characters in each word of series:")
print(result)
				
			

a) 5, 5, 4, 7
b) 1, 1, 1, 1
c) 4, 5, 4, 7
d) 0, 0, 0, 0

Correct answer is: a) 5, 5, 4, 7
Explanation: The code creates a Pandas Ser

Python Pandas MCQ : Set 3

Python Pandas MCQ

1). What is the purpose of the `value_counts()` function in Pandas?

a) To calculate the cumulative sum of a column
b) To count the occurrences of each unique value in a column
c) To sort a DataFrame based on a specific column
d) To remove duplicate rows from a DataFrame

Correct answer is: b) To count the occurrences of each unique value in a column
Explanation: The `value_counts()` function in Pandas is used to count the occurrences of each unique value in a column.

2). How can you calculate the difference between two dates in Pandas?

a) df.diff_dates()
b) df.difference()
c) df.calculate_difference()
d) df.subtract_dates()

Correct answer is: a) df.diff_dates()
Explanation: The `diff_dates()` function is used to calculate the difference between two dates in Pandas.

3). What is the purpose of the `fillna()` function in Pandas?

a) To remove missing values from a DataFrame
b) To replace missing values with a specified value
c) To interpolate missing values in a DataFrame
d) To drop rows with missing values

Correct answer is: b) To replace missing values with a specified value
Explanation: The `fillna()` function is used to replace missing values in a DataFrame with a specified value in Pandas.

4). How can you calculate the median of each column in a DataFrame in Pandas?

a) df.median()
b) df.calculate_median()
c) df.column_median()
d) df.median_column()

Correct answer is: a) df.median()
Explanation: The `median()` function in Pandas is used to calculate the median of each column in a DataFrame.

5). What is the purpose of the `rename()` function in Pandas?

a) To calculate the mean of each column in a DataFrame
b) To remove duplicate rows from a DataFrame
c) To change the index labels of a DataFrame
d) To sort a DataFrame based on a specific column

Correct answer is: c) To change the index labels of a DataFrame
Explanation: The `rename()` function in Pandas is used to change the index labels of a DataFrame.

6). How can you calculate the mode of each column in a DataFrame in Pandas?

a) df.mode()
b) df.calculate_mode()
c) df.column_mode()
d) df.mode_column()

Correct answer is: a) df.mode()
Explanation: Each column in a DataFrame’s mode is determined using the Pandas’mode()’ function.

7). What is the purpose of the `cumprod()` function in Pandas?

a) To calculate the cumulative sum of a column
b) To calculate the cumulative product of a column
c) To calculate the cumulative mean of a column
d) To calculate the cumulative median of a column

Correct answer is: b) To calculate the cumulative product of a column
Explanation: The `cumprod()` function in Pandas is used to calculate the cumulative product of a column.

8). How can you calculate the maximum value of each column in a DataFrame in Pandas?

a) df.max()
b) df.maximum()
c) df.calculate_max()
d) df.column_max()

Correct answer is: a) df.max()
Explanation: The `max()` function in Pandas is used to calculate the maximum value of each column in a DataFrame.

9). What is the purpose of the `cummin()` function in Pandas?

a) To calculate the cumulative sum of a column
b) To calculate the cumulative minimum of a column
c) To calculate the cumulative mean of a column
d) To calculate the cumulative median of a column

Correct answer is: b) To calculate the cumulative minimum of a column
Explanation: The `cummin()` function in Pandas is used to calculate the cumulative minimum of a column.

10). How can you calculate the minimum value of each column in a DataFrame in Pandas?

a) df.min()
b) df.minimum()
c) df.calculate_min()
d) df.column_min()

Correct answer is: a) df.min()
Explanation: The `min()` function in Pandas is used to calculate the minimum value of each column in a DataFrame.

11). How can you select multiple columns from a DataFrame in Pandas?

a) df.select_columns()
b) df.columns()
c) df.get_columns()
d) df[[‘column1’, ‘column2’]]

Correct answer is: d) df[[‘column1’, ‘column2’]]
Explanation: To select multiple columns from a DataFrame in Pandas, you can use the double square bracket notation.

12). How can you calculate the standard deviation of each column in a DataFrame in Pandas?

a) df.std()
b) df.standard_deviation()
c) df.calculate_std()
d) df.column_std()

Correct answer is: a) df.std()
Explanation: The `std()` function in Pandas is used to calculate the standard deviation of each column in a DataFrame.

13). What is the purpose of the `agg()` function in Pandas?

a) To aggregate data based on a specific column
b) To determine a column’s average value
c) To remove rows with empty values
d) To sort a DataFrame based on a specific column

Correct answer is: a) To aggregate data based on a specific column
Explanation: The `agg()` function in Pandas is used to aggregate data based on a specific column using a specified function or a dictionary of functions.

14). How can you calculate the skewness of each column in a DataFrame in Pandas?

a) df.skew()
b) df.calculate_skewness()
c) df.column_skewness()
d) df.skewness()

Correct answer is: a) df.skew()
Explanation: The `skew()` function in Pandas is used to calculate the skewness of each column in a DataFrame.

15). How can you calculate the kurtosis of each column in a DataFrame in Pandas?

a) df.kurtosis()
b) df.calculate_kurtosis()
c) df.column_kurtosis()
d) df.kurt()

Correct answer is: a) df.kurtosis()
Explanation: The `kurtosis()` function in Pandas is used to calculate the kurtosis of each column in a DataFrame.

16). What is the purpose of the `iterrows()` function in Pandas?

a) To iterate over the rows of a DataFrame
b) To iterate over the columns of a DataFrame
c) To iterate over the unique values of a column
d) To iterate over the index labels of a DataFrame

Correct answer is: a) To iterate over the rows of a DataFrame
Explanation: The `iterrows()` function in Pandas is used to iterate over the rows of a DataFrame.

17). How can you calculate the covariance between two columns in a DataFrame in Pandas?

a) df.calculate_covariance()
b) df.column_covariance()
c) df.cov()
d) df.covariance()

Correct answer is: c) df.cov()
Explanation: The `cov()` function in Pandas is used to calculate the covariance between two columns in a DataFrame.

18). What is the purpose of the `pivot()` function in Pandas?

a) To transpose the rows and columns of a DataFrame
b) To calculate the average value of a column in a DataFrame
c) To create a summary table based on a DataFrame’s columns
d) To reshape the structure of a DataFrame

Correct answer is: d) To reshape the structure of a DataFrame
Explanation: The `pivot()` function in Pandas is used to reshape the structure of a DataFrame based on the values of a column.

19). How can you calculate the percentile of each column in a DataFrame in Pandas?

a) df.percentile()
b) df.calculate_percentile()
c) df.column_percentile()
d) df.quantile()

Correct answer is: d) df.quantile()
Explanation: The `quantile()` function in Pandas is used to calculate the percentile of each column in a DataFrame.

20). What is the purpose of the `rolling()` function in Pandas?

a) To calculate rolling statistics on a column
b) To remove duplicate rows from a DataFrame
c) To interpolate missing values in a DataFrame
d) To calculate the cumulative sum of a column

Correct answer is: a) To calculate rolling statistics on a column
Explanation: The `rolling()` function in Pandas is used to calculate rolling statistics, such as the rolling mean or rolling sum, on a column.

21). How can you calculate the exponential moving average of a column in a DataFrame in Pandas?

a) df.calculate_ema()
b) df.ema()
c) df.exp_moving_average()
d) df.ewm()

Correct answer is: d) df.ewm()
Explanation: The `ewm()` function in Pandas is used to calculate the exponential moving average of a column in a DataFrame.

22). What is the purpose of the `stack()` function in Pandas?

a) To stack multiple DataFrames vertically
b) To stack multiple DataFrames horizontally
c) To stack multiple columns into a single column
d) To stack multiple rows into a single row

Correct answer is: c) To stack multiple columns into a single column
Explanation: The `stack()` function in Pandas is used to stack multiple columns into a single column.

23). How can you calculate the weighted average of a column in a DataFrame in Pandas?

a) df.calculate_weighted_average()
b) df.weighted_avg()
c) df.column_weighted_average()
d) df.dot_product()

Correct answer is: b) df.weighted_avg()
Explanation: There is no built-in weighted average function in Pandas. However, you can calculate the weighted average by multiplying the values of a column by their corresponding weights and then dividing the sum by the sum of the weights.

24). What is the purpose of the `squeeze()` function in Pandas?

a) To remove a single-dimensional axis from a DataFrame
b) To compress the data in a DataFrame to reduce memory usage
c) To remove duplicate rows from a DataFrame
d) To sort a DataFrame based on a specific column

Correct answer is: a) To remove a single-dimensional axis from a DataFrame
Explanation: The `squeeze()` function in Pandas is used to remove a single-dimensional axis from a DataFrame, resulting in a Series if applicable.

25). What is the purpose of the melt() function in Pandas?

a) To merge multiple DataFrames based on a common column
b) To transpose the rows and columns of a DataFrame
c) To reshape a DataFrame from wide to long format
d) To calculate the median of each column in a DataFrame

Correct answer is: c) To reshape a DataFrame from wide to long format
Explanation: The melt() function in Pandas is used to reshape a DataFrame from wide to long format by unpivoting the data based on specified columns.