KVS PGT Computer Science Exam Preparation: Pandas MCQs with Explanations

Post a Comment

These questions are designed for the upcoming KVS PGT Computer Science exam, with a focus on the Pandas library in Python. The material includes ten challenging multiple-choice questions on various aspects of Pandas, from data manipulation to data analysis. Each question includes a detailed explanation of the solution, making it an ideal resource for both students and teachers preparing for the exam. The questions cover a range of topics and are designed to test your knowledge and understanding of Pandas. Start preparing today to enhance your skills and increase your chances of success on the exam.


KVS Pandas MCQs with Explanations



1. What is the result of the following code snippet in Pandas?

import pandas as pd

data = {'col1': [1, 2, 3, 4], 'col2': [5, 6, 7, 8]}

df = pd.DataFrame(data)

print(df.loc[df['col1'] >= 3, 'col2'].sum())

A) 7

B) 15

C) 20

D) 28

The correct answer is B) 15.

explanation:

The code imports the Pandas library as pd and creates a DataFrame df with two columns col1 and col2 and their respective values.

Then, the line print(df.loc[df['col1'] >= 3, 'col2'].sum()) selects the values of the col2 column where the values of col1 are greater than or equal to 3. This selection is done using the df.loc method and the condition df['col1'] >= 3. The sum() method then calculates the sum of the selected values in the col2 column.

So, the selected values of col2 are [7, 8], and their sum is 7 + 8 = 15. Hence, the correct answer is B) 15.


2. What is the result of the following code in Pandas?


import pandas as pd

data = {'col1': [1, 2, 3, 4], 'col2': [5, 6, 7, 8]}

df = pd.DataFrame(data)

print(df.sort_values(by='col1', ascending=False))

A) Sorted values of col1 in ascending order

B) Sorted values of col1 in descending order

C) Sorted values of col2 in ascending order

D) Sorted values of col2 in descending order


Answer: B) Sorted values of col1 in descending order. The sort_values method sorts the DataFrame by the values of the specified column (col1 in this case) in the specified order (descending, ascending=False).


3. What is the result of the following code in Pandas?

import pandas as pd

data = {'col1': [1, 2, 3, 4], 'col2': [5, 6, 7, 8]}

df = pd.DataFrame(data)

print(df.head(2))

A) First two rows of col1

B) First two rows of col2

C) First two rows of both col1 and col2

D) Last two rows of both col1 and col2


Answer: C) First two rows of both col1 and col2. The head method returns the first n rows (in this case, n=2) of the DataFrame.


4. What is the result of the following code in Pandas?

import pandas as pd

data = {'col1': [1, 2, 3, 4], 'col2': [5, 6, 7, 8]}

df = pd.DataFrame(data)

print(df['col1'].mean())

A) 1

B) 2.5

C) 3

D) 4


Answer: B) 2.5. The mean method calculates the mean value of the col1 column. The mean of [1, 2, 3, 4] is (1 + 2 + 3 + 4) / 4 = 2.5.



5. What is the result of the following code in Pandas?

import pandas as pd

data = {'col1': [1, 2, 3, 4], 'col2': [5, 6, 7, 8]}

df = pd.DataFrame(data)

print(df['col1'].unique())

A) All unique values of col1

B) All unique values of col2

C) All unique values of both col1 and col2

D) Mean of unique values of col1


Answer: A) All unique values of col1. The unique method returns an array of unique values in the specified column (col1 in this case).


6. What is the result of the following code in Pandas?

import pandas as pd

data = {'col1': [1, 2, 3, 4], 'col2': [5, 6, 7, 8]}

df = pd.DataFrame(data)

df['col3'] = df['col1'] + df['col2']

print(df)

A) New DataFrame with an added column col3

B) New DataFrame with the sum of the values of col1 and col2

C) Error due to conflicting column names

D) Error due to incompatible data types


Answer: A) New DataFrame with an added column col3. The code creates a new column col3 in the DataFrame by adding the values of col1 and col2 and stores it in the DataFrame.


7. What is the result of the following code in Pandas?

import pandas as pd

data = {'col1': [1, 2, 3, 4], 'col2': [5, 6, 7, 8]}

df = pd.DataFrame(data)

df.drop(2, axis=0, inplace=True)

print(df)

A) DataFrame with the third row removed

B) DataFrame with the second row removed

C) DataFrame with the first row removed

D) DataFrame with all rows removed


Answer: A) DataFrame with the third row removed. The drop method removes the specified row (row 2 in this case) from the DataFrame. The axis argument specifies the axis along which the values are dropped (0 for rows, 1 for columns), and the inplace argument specifies whether the modification is made in place (inplace=True) or whether a new DataFrame is returned (inplace=False).


8. What is the result of the following code in Pandas?

import pandas as pd

data = {'col1': [1, 2, 3, 4], 'col2': [5, 6, 7, 8]}

df = pd.DataFrame(data)

df.rename(columns={'col1': 'column_1', 'col2': 'column_2'}, inplace=True)

print(df.columns)

A) Error due to conflicting column names

B) Error due to invalid argument

C) List of original column names

D) List of new column names


Answer: D) List of new column names. The rename method renames the specified columns (col1 to column_1 and col2 to column_2 in this case). The columns attribute returns the names of all columns in the DataFrame, which will be the new names after renaming.


9. What is the result of the following code in Pandas?

import pandas as pd

data = {'col1': [1, 2, 3, 4], 'col2': [5, 6, 7, 8]}

df = pd.DataFrame(data)

df.sort_values(by='col1', ascending=False, inplace=True)

print(df)

A) DataFrame sorted in descending order by col1

B) DataFrame sorted in ascending order by col1

C) DataFrame sorted in descending order by col2

D) DataFrame sorted in ascending order by col2


Answer: A) DataFrame sorted in descending order by col1. The sort_values method sorts the DataFrame by the values in the specified column (col1 in this case). The ascending argument specifies the order of the sorting (ascending=False for descending, ascending=True for ascending).


10. What is the result of the following code in Pandas?

import pandas as pd

data = {'col1': [1, 2, 3, 4], 'col2': [5, 6, 7, 8]}

df = pd.DataFrame(data)

df.set_index('col1', inplace=True)

print(df)

A) DataFrame with col1 as the index

B) DataFrame with col2 as the index

C) DataFrame with both col1 and col2 as the index

D) Error due to conflicting index names


Answer: A) DataFrame with col1 as the index. The set_index method sets the specified column (col1 in this case) as the index of the DataFrame.



Related Posts

Post a Comment

Subscribe Our Newsletter