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

Leave a Comment