CBSE Class 12
Informatics Practices
03 Data Handling using Pandas - II
50
Total
0
Attempted
0
Correct
0
Wrong
00:00:00
Q-1
Which statistical value is represented by the result of the following code?
Easy
Descriptive Statistics
Sample Data
Data values: [10, 20, 30, 40]
Reference Code
import pandas as pd
s = pd.Series([10, 20, 30, 40])
print(s.mean())
A
Median
B
Mean
C
Mode
D
Range
Answer: Mean
Explanation: The mean() function calculates the average value of the data.
Q-2
What does the following function return?
Easy
Descriptive Statistics
Sample Data
Series values: [5, 15, 25]
Reference Code
print(s.count())
A
Sum of values
B
Number of non-missing values
C
Total number of rows including missing values
D
Maximum value
Answer: Number of non-missing values
Explanation: count() returns the number of non-NULL (non-missing) values.
Q-3
Which operation is being performed by the following code?
Easy
Data Aggregations
Sample Data
| Name | Score |
|---|---|
| A | 80 |
| B | 70 |
| C | 90 |
Reference Code
print(df['Score'].sum())
A
Finding average score
B
Finding total score
C
Finding highest score
D
Finding lowest score
Answer: Finding total score
Explanation: The sum() function adds all values in the column.
Q-4
What will be the order of rows after executing the following code?
Easy
Sorting DataFrame
Sample Data
| Name | Marks |
|---|---|
| A | 65 |
| B | 85 |
| C | 75 |
Reference Code
print(df.sort_values(by='Marks'))
A
B, C, A
B
A, C, B
C
C, A, B
D
Original order
Answer: A, C, B
Explanation: sort_values() arranges rows in ascending order of Marks by default.
Q-5
Which result is produced by the following GROUP BY operation?
Easy
GROUP BY Functions
Sample Data
| Class | Marks |
|---|---|
| XI | 70 |
| XI | 80 |
| XII | 90 |
Reference Code
df.groupby('Class').mean()
A
Average marks for each class
B
Total marks for each class
C
Highest marks for each class
D
All individual records
Answer: Average marks for each class
Explanation: groupby().mean() calculates average values for each group.
Q-6
What is the effect of the following statement?
Easy
Altering the Index
Sample Data
DataFrame index: 0, 1, 2
Reference Code
df.set_index('RollNo', inplace=True)
A
Creates a new DataFrame
B
Deletes the RollNo column
C
Changes index to RollNo
D
Sorts data by RollNo
Answer: Changes index to RollNo
Explanation: set_index() replaces the existing index with the specified column.
Q-7
Which operation is being performed by the following code?
Easy
Other DataFrame Operations
Sample Data
| A | B |
|---|---|
| 1 | 4 |
| 2 | 5 |
Reference Code
print(df.describe())
A
Sorting the DataFrame
B
Displaying structure information
C
Displaying statistical summary
D
Removing missing values
Answer: Displaying statistical summary
Explanation: describe() provides a statistical summary of numerical columns.
Q-8
Which rows are removed by the following statement?
Easy
Handling Missing Values
Sample Data
| Name | Marks |
|---|---|
| A | 80 |
| B | NaN |
| C | 70 |
Reference Code
df.dropna()
A
Rows with Marks less than 75
B
Rows with missing values
C
Rows with highest values
D
No rows are removed
Answer: Rows with missing values
Explanation: dropna() removes rows containing missing (NaN) values.
Q-9
Which method is used to replace missing values with a specified value?
Easy
Handling Missing Values
A
dropna()
B
fillna()
C
replace()
D
isnull()
Answer: fillna()
Explanation: fillna() replaces missing values with a given value.
Q-10
Which operation is being performed by the following code?
Easy
Pandas MySQL Integration
Reference Code
df.to_sql('student', con=conn, if_exists='replace')
A
Reading data from MySQL
B
Exporting DataFrame to MySQL
C
Sorting database records
D
Deleting MySQL table
Answer: Exporting DataFrame to MySQL
Explanation: to_sql() writes DataFrame data into a MySQL table.
Q-11
Which statistical measure is returned by the following code?
Medium
Descriptive Statistics
Sample Data
Data values: [12, 15, 18, 20]
Reference Code
import pandas as pd
s = pd.Series([12, 15, 18, 20])
print(s.median())
A
Mean
B
Median
C
Mode
D
Standard Deviation
Answer: Median
Explanation: median() returns the middle value (or average of two middle values) of the sorted data.
Q-12
What does the following code compute?
Medium
Descriptive Statistics
Sample Data
| Name | Score |
|---|---|
| A | 50 |
| B | 70 |
| C | 90 |
Reference Code
print(df['Score'].std())
A
Average score
B
Spread of score values
C
Highest score
D
Difference between max and min
Answer: Spread of score values
Explanation: std() computes the standard deviation, which measures the spread of data values.
Q-13
Which result is produced by the following aggregation?
Medium
Data Aggregations
Sample Data
| Dept | Salary |
|---|---|
| IT | 40000 |
| IT | 30000 |
| HR | 35000 |
Reference Code
df.groupby('Dept')['Salary'].sum()
A
Average salary per department
B
Total salary per department
C
Highest salary per department
D
Individual salaries
Answer: Total salary per department
Explanation: groupby().sum() adds salaries within each department group.
Q-14
How will the DataFrame rows be ordered after execution?
Medium
Sorting DataFrame
Sample Data
| Name | Age |
|---|---|
| A | 18 |
| B | 16 |
| C | 17 |
Reference Code
print(df.sort_values(by='Age', ascending=False))
A
B, C, A
B
A, C, B
C
C, A, B
D
Original order
Answer: A, C, B
Explanation: Rows are sorted in descending order of Age: 18, 17, 16.
Q-15
Which output is generated by the following group operation?
Medium
GROUP BY Functions
Sample Data
| Class | Marks |
|---|---|
| XI | 60 |
| XI | 80 |
| XII | 70 |
Reference Code
df.groupby('Class').count()
A
Number of non-missing values per class
B
Total marks per class
C
Average marks per class
D
Original DataFrame
Answer: Number of non-missing values per class
Explanation: count() returns the number of non-missing values for each group.
Q-16
What will be the effect of the following statement?
Medium
Altering the Index
Sample Data
Current DataFrame index: RollNo
Reference Code
df.reset_index(inplace=True)
A
Deletes the index permanently
B
Moves index back to a column
C
Sorts rows by index
D
Removes duplicate rows
Answer: Moves index back to a column
Explanation: reset_index() converts the index into a column and restores default integer index.
Q-17
Which information is displayed by the following method?
Medium
Other DataFrame Operations
Reference Code
print(df.info())
A
Statistical summary
B
Row-wise data
C
Column names, data types, and non-null counts
D
Sorted DataFrame
Answer: Column names, data types, and non-null counts
Explanation: info() displays structure-related information of the DataFrame.
Q-18
Which rows remain after executing the following code?
Medium
Handling Missing Values
Sample Data
| Name | Marks |
|---|---|
| A | 80 |
| B | NaN |
| C | 70 |
| D | NaN |
Reference Code
df.dropna(subset=['Marks'])
A
Only rows with NaN values
B
Rows A and C
C
Rows B and D
D
All rows
Answer: Rows A and C
Explanation: dropna(subset=...) removes rows where specified column has missing values.
Q-19
What does the following statement achieve?
Medium
Handling Missing Values
Reference Code
df.fillna(df.mean())
A
Deletes missing values
B
Replaces missing values with column means
C
Sorts the DataFrame
D
Converts data types
Answer: Replaces missing values with column means
Explanation: fillna(df.mean()) fills missing values using the mean of each column.
Q-20
Which operation is being performed by the following code?
Medium
Pandas MySQL Integration
Reference Code
df = pd.read_sql('SELECT * FROM student', con=conn)
A
Exporting DataFrame to MySQL
B
Importing MySQL table into DataFrame
C
Sorting MySQL records
D
Deleting MySQL table
Answer: Importing MySQL table into DataFrame
Explanation: read_sql() reads data from a MySQL query into a Pandas DataFrame.
Q-21
Which statistic is computed by the following code, and on which data?
Medium
GROUP BY Functions
Sample Data
| City | Temp |
|---|---|
| A | 30 |
| A | 32 |
| B | 28 |
| B | 26 |
Reference Code
import pandas as pd
result = df.groupby('City')['Temp'].mean()
A
Overall mean temperature
B
Mean temperature per city
C
Maximum temperature per city
D
Count of records per city
Answer: Mean temperature per city
Explanation: Grouping by City and applying mean() computes the average temperature for each city.
Q-22
What does the following code return?
Medium
Descriptive Statistics
Sample Data
Numeric column with values including missing entries
Reference Code
stats = df['Score'].describe()
A
Only mean and standard deviation
B
Statistical summary including count, mean, std, min, quartiles, max
C
Only minimum and maximum values
D
Row-wise descriptive data
Answer: Statistical summary including count, mean, std, min, quartiles, max
Explanation: describe() returns a comprehensive statistical summary for numeric data.
Q-23
How does the following aggregation behave?
Medium
Data Aggregations
Sample Data
| Dept | Salary |
|---|---|
| IT | 50000 |
| IT | NaN |
| HR | 40000 |
Reference Code
df.groupby('Dept')['Salary'].count()
A
Counts total rows per department
B
Counts non-missing salary values per department
C
Sums salary per department
D
Ignores departments with missing values
Answer: Counts non-missing salary values per department
Explanation: count() counts only non-missing values within each group.
Q-24
What will be the order of rows after sorting?
Medium
Sorting DataFrame
Sample Data
| Name | Score |
|---|---|
| A | 70 |
| B | 85 |
| C | 70 |
Reference Code
sorted_df = df.sort_values(by=['Score','Name'])
A
B, A, C
B
A, C, B
C
C, A, B
D
A, B, C
Answer: A, C, B
Explanation: Rows are sorted by Score ascending, and ties are resolved by Name alphabetically.
Q-25
What is the effect of the following index operation?
Medium
Altering the Index
Sample Data
Current index is default integer index
Reference Code
df.set_index('ID', inplace=False)
A
Permanently changes the index of df
B
Returns a new DataFrame with ID as index
C
Deletes the ID column from df
D
Sorts df by ID
Answer: Returns a new DataFrame with ID as index
Explanation: With inplace=False, set_index returns a new DataFrame without modifying the original.
Q-26
Which information is best inferred from the following output?
Medium
Other DataFrame Operations
Sample Data
DataFrame with mixed numeric columns
Reference Code
print(df.corr())
A
Row-wise differences
B
Correlation between numeric columns
C
Frequency of values
D
Sorted order of columns
Answer: Correlation between numeric columns
Explanation: corr() computes pairwise correlation between numeric columns.
Q-27
Which rows are affected by the following operation?
Medium
Handling Missing Values
Sample Data
| Name | Marks |
|---|---|
| A | NaN |
| B | 60 |
| C | NaN |
Reference Code
df.fillna(0)
A
Only rows with Marks < 60
B
Only rows with missing values
C
All rows
D
No rows
Answer: Only rows with missing values
Explanation: fillna(0) replaces missing values while leaving existing values unchanged.
Q-28
What will be the result of the following chained operation?
Medium
GROUP BY Functions
Sample Data
| Dept | Sales |
|---|---|
| A | 100 |
| A | 200 |
| B | 150 |
Reference Code
df.groupby('Dept')['Sales'].mean().sort_values(ascending=False)
A
Departments sorted by total sales
B
Departments sorted by average sales descending
C
Original order with averages
D
Row-wise sorted sales
Answer: Departments sorted by average sales descending
Explanation: The code computes mean sales per department and sorts them in descending order.
Q-29
What is the primary effect of the following code?
Medium
Pandas MySQL Integration
Sample Data
Existing DataFrame with table ‘employees’ in MySQL
Reference Code
df.to_sql('employees', con=conn, if_exists='append')
A
Replaces the existing table
B
Deletes existing records
C
Adds DataFrame rows to the existing table
D
Creates a temporary table
Answer: Adds DataFrame rows to the existing table
Explanation: if_exists='append' adds rows to the existing table without deleting data.
Q-30
Which conclusion is correct based on the following code?
Medium
Other DataFrame Operations
Sample Data
DataFrame with numeric column ‘Marks’
Reference Code
filtered = df[df['Marks'] > df['Marks'].mean()]
A
Rows with marks equal to the mean are selected
B
Rows above the average marks are selected
C
Rows below the average marks are selected
D
All rows are selected
Answer: Rows above the average marks are selected
Explanation: Boolean indexing selects rows where Marks exceed the column mean.
Q-31
What will be the result of the following code?
Hard
GROUP BY Functions
Sample Data
| Dept | Salary |
|---|---|
| IT | 40000 |
| IT | NaN |
| HR | 30000 |
| HR | 30000 |
Reference Code
result = df.groupby('Dept')['Salary'].mean()
A
IT: 20000, HR: 30000
B
IT: 40000, HR: 30000
C
IT: NaN, HR: 30000
D
IT: 80000, HR: 60000
Answer: IT: 40000, HR: 30000
Explanation: mean() ignores NaN values. IT has one valid value (40000), HR average is (30000+30000)/2.
Q-32
How many rows will remain after executing the following code?
Hard
Handling Missing Values
Sample Data
| Name | Marks |
|---|---|
| A | 80 |
| B | NaN |
| C | 70 |
| D | NaN |
Reference Code
df2 = df.dropna()
A
4
B
3
C
2
D
1
Answer: 2
Explanation: dropna() removes all rows containing at least one missing value. Only A and C remain.
Q-33
Which rows will appear in the final output?
Hard
Sorting DataFrame
Sample Data
| Student | Score |
|---|---|
| A | 60 |
| B | 80 |
| C | 70 |
| D | 90 |
Reference Code
result = df.sort_values('Score').tail(2)
A
A and C
B
B and D
C
C and D
D
A and D
Answer: C and D
Explanation: After sorting ascending, tail(2) selects the two highest scores: 70 and 90 (C and D).
Q-34
What is the shape of the resulting DataFrame?
Hard
GROUP BY Functions
Sample Data
| Dept | Year | Sales |
|---|---|---|
| A | 2022 | 100 |
| A | 2023 | 150 |
| B | 2022 | 120 |
Reference Code
result = df.groupby(['Dept','Year']).sum()
A
(3, 1)
B
(2, 2)
C
(3, 3)
D
(2, 1)
Answer: (3, 1)
Explanation: There are three unique (Dept, Year) groups, and one aggregated column (Sales).
Q-35
What will be the effect of the following operation?
Hard
Altering the Index
Sample Data
Index currently set to ‘RollNo’
Reference Code
df.reset_index(drop=True, inplace=True)
A
Moves index to a column
B
Deletes the existing index and creates a new default index
C
Sorts rows by RollNo
D
Removes duplicate rows
Answer: Deletes the existing index and creates a new default index
Explanation: reset_index(drop=True) removes the current index and restores a default integer index.
Q-36
Which statistical value is affected MOST by an extreme outlier?
Hard
Descriptive Statistics
Sample Data
Values: [10, 12, 14, 1000]
Reference Code
s.mean(), s.median()
A
Median only
B
Mean only
C
Both equally
D
Neither
Answer: Mean only
Explanation: Mean is highly sensitive to extreme values, while median is more robust.
Q-37
Which rows are selected by the following chained operation?
Hard
Other DataFrame Operations
Sample Data
| Name | Marks |
|---|---|
| A | 40 |
| B | 60 |
| C | 80 |
| D | 100 |
Reference Code
result = df[df['Marks'] > df['Marks'].median()]
A
A and B
B
B and C
C
C and D
D
A and D
Answer: C and D
Explanation: Median is (60+80)/2 = 70. Rows with Marks > 70 are C and D.
Q-38
What will be the output index type after this operation?
Hard
Altering the Index
Sample Data
Grouped DataFrame with multi-level index
Reference Code
result = df.groupby(['Dept','Year']).sum().reset_index()
A
MultiIndex
B
Single integer index
C
Index based on Dept only
D
Index based on Year only
Answer: Single integer index
Explanation: reset_index() removes the MultiIndex and replaces it with a default integer index.
Q-39
What is the primary risk of the following operation?
Hard
Pandas MySQL Integration
Sample Data
Existing MySQL table ‘sales’
Reference Code
df.to_sql('sales', con=conn, if_exists='replace')
A
Duplicate records
B
Loss of existing table data
C
Index mismatch
D
Type conversion error
Answer: Loss of existing table data
Explanation: if_exists='replace' drops the existing table and recreates it, causing data loss.
Q-40
Which conclusion is correct for the following operation?
Hard
Descriptive Statistics
Sample Data
| A | B |
|---|---|
| 1 | 10 |
| 2 | NaN |
| 3 | 30 |
Reference Code
result = df.mean()
A
NaN propagates and mean is NaN
B
Mean ignores NaN values
C
Mean replaces NaN with 0
D
Mean cannot be computed
Answer: Mean ignores NaN values
Explanation: Pandas statistical functions ignore NaN values by default.
Q-41
What will be the output of the following chained operation?
Hard
GROUP BY Functions
Sample Data
| Dept | Sales |
|---|---|
| A | 100 |
| A | 200 |
| B | 150 |
| B | NaN |
Reference Code
result = df.groupby('Dept')['Sales'].mean().fillna(0)
A
A: 150, B: 0
B
A: 300, B: 150
C
A: 150, B: 150
D
A: 0, B: 150
Answer: A: 150, B: 150
Explanation: Mean ignores NaN. A mean = (100+200)/2 = 150; B mean = 150. fillna(0) has no effect here.
Q-42
How many rows will remain after executing the following code?
Hard
Other DataFrame Operations
Sample Data
| ID | Score |
|---|---|
| 1 | 45 |
| 2 | 55 |
| 3 | NaN |
| 4 | 75 |
Reference Code
df2 = df[df['Score'] > df['Score'].mean()]
A
1
B
2
C
3
D
4
Answer: 2
Explanation: Mean ignores NaN: (45+55+75)/3 ≈ 58.33. Only Score 75 is above the mean.
Q-43
What will be the resulting index after the following operation?
Hard
GROUP BY Functions
Sample Data
Data grouped by two columns
Reference Code
result = df.groupby(['Dept','Year']).sum()
A
Default integer index
B
Single-level index with Dept
C
MultiIndex with Dept and Year
D
Index removed
Answer: MultiIndex with Dept and Year
Explanation: Grouping by multiple columns creates a MultiIndex by default.
Q-44
Which rows will be selected by the following sorting logic?
Hard
Sorting DataFrame
Sample Data
| Name | Marks |
|---|---|
| A | 70 |
| B | 90 |
| C | 80 |
| D | 60 |
Reference Code
result = df.sort_values('Marks', ascending=False).head(2)
A
A and C
B
B and C
C
B and A
D
C and D
Answer: B and C
Explanation: Sorting descending gives 90, 80, 70, 60. head(2) selects B and C.
Q-45
What is the effect of the following operation on missing values?
Hard
Handling Missing Values
Sample Data
| A | B |
|---|---|
| 1 | NaN |
| 2 | 5 |
| 3 | NaN |
Reference Code
result = df.fillna(df.median())
A
Replaces NaN with column means
B
Replaces NaN with column medians
C
Drops rows with NaN
D
Leaves NaN unchanged
Answer: Replaces NaN with column medians
Explanation: fillna(df.median()) fills missing values using the median of each column.
Q-46
What will be the number of rows in the resulting DataFrame?
Hard
GROUP BY Functions
Sample Data
| Dept | Year | Profit |
|---|---|---|
| A | 2022 | 50 |
| A | 2023 | 60 |
| A | 2023 | 70 |
Reference Code
result = df.groupby(['Dept','Year'])['Profit'].count().reset_index()
A
1
B
2
C
3
D
4
Answer: 2
Explanation: There are two unique (Dept, Year) groups: (A,2022) and (A,2023). reset_index does not change row count.
Q-47
Which statement correctly explains the result of the following code?
Hard
Descriptive Statistics
Sample Data
Numeric DataFrame with missing values
Reference Code
stats = df.describe()
A
Includes NaN rows in all calculations
B
Ignores NaN values in statistics
C
Drops columns with NaN
D
Returns only count and mean
Answer: Ignores NaN values in statistics
Explanation: describe() ignores NaN values when computing statistics.
Q-48
What will be the primary outcome of the following database operation?
Hard
Pandas MySQL Integration
Sample Data
Existing MySQL table ‘students’ with records
Reference Code
df.to_sql('students', con=conn, if_exists='append', index=False)
A
Existing table is replaced
B
New rows are added without index column
C
Index column becomes primary key
D
Table structure is dropped
Answer: New rows are added without index column
Explanation: append adds new rows and index=False prevents writing the index as a column.
Q-49
Which rows will remain after the following two-step operation?
Hard
Other DataFrame Operations
Sample Data
| ID | Score |
|---|---|
| 1 | 40 |
| 2 | 60 |
| 3 | NaN |
| 4 | 80 |
Reference Code
df2 = df.dropna()
df3 = df2[df2['Score'] >= df2['Score'].mean()]
A
Only ID 4
B
ID 2 and ID 4
C
ID 1, 2, and 4
D
Only ID 2
Answer: ID 2 and ID 4
Explanation: After dropna, scores are 40, 60, 80. Mean = 60. Rows with Score ≥ 60 are IDs 2 and 4.
Q-50
Which conclusion is correct about the following chained computation?
Hard
Descriptive Statistics
Sample Data
| Value |
|---|
| 10 |
| 20 |
| NaN |
| 40 |
Reference Code
result = df['Value'].mean() * df['Value'].count()
A
Equals sum of all values including NaN
B
Equals sum of non-missing values
C
Equals median multiplied by count
D
Produces incorrect result due to NaN
Answer: Equals sum of non-missing values
Explanation: mean() and count() both ignore NaN. Their product equals the sum of non-missing values.
◀
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
▶