CBSE Class 12
Informatics Practices
02 Data Handling using Pandas - I
50
Total
0
Attempted
0
Correct
0
Wrong
00:00:00
Q-1
Which Python library is primarily used for data analysis and handling tabular data?
Easy
Python Libraries
Answer: Pandas
Explanation: Pandas is specifically designed for data analysis and handling structured data.
Q-2
Which data structure in Pandas is one-dimensional?
Easy
Pandas Series
Answer: Series
Explanation: A Pandas Series is a one-dimensional labeled array.
Q-3
How many elements are present in the following Series?
Easy
Pandas Series
Sample Data
s = [10, 20, 30, 40]
Reference Code
import pandas as pd
s = pd.Series([10, 20, 30, 40])
Answer: 4
Explanation: The Series is created from a list containing four values.
Q-4
Which attribute is used to display the values of a Pandas Series?
Easy
Pandas Series
Answer: values
Explanation: The values attribute returns the data stored in the Series.
Q-5
Which object represents rows and columns in Pandas?
Easy
Pandas DataFrame
Answer: DataFrame
Explanation: A DataFrame is a two-dimensional structure with rows and columns.
Q-6
How many columns are present in the given DataFrame?
Easy
Pandas DataFrame
Sample Data
| Name | Age | Marks |
|---|---|---|
| Anu | 16 | 85 |
| Ravi | 17 | 78 |
Reference Code
import pandas as pd
df = pd.DataFrame({
'Name': ['Anu', 'Ravi'],
'Age': [16, 17],
'Marks': [85, 78]
})
Answer: 3
Explanation: The DataFrame has three columns: Name, Age, and Marks.
Q-7
Which function is used to read data from a CSV file into a DataFrame?
Easy
CSV Import Export
Answer: read_csv()
Explanation: The read_csv() function is used to read CSV files into Pandas DataFrames.
Q-8
Which function is used to write a DataFrame to a CSV file?
Easy
CSV Import Export
Answer: to_csv()
Explanation: The to_csv() function writes a DataFrame to a CSV file.
Q-9
Which library provides the ndarray data structure?
Easy
Pandas vs NumPy
Answer: NumPy
Explanation: NumPy provides the ndarray data structure for numerical computing.
Q-10
Which statement correctly distinguishes a Pandas Series from a NumPy ndarray?
Easy
Pandas vs NumPy
Answer: Series supports labels while ndarray does not
Explanation: A Pandas Series has index labels, whereas a NumPy ndarray does not support labels.
Q-11
What will be the index values of the Series created by the code?
Medium
Pandas Series
Sample Data
Data values: [100, 200, 300]
Reference Code
import pandas as pd
s = pd.Series([100, 200, 300])
Answer: [0, 1, 2]
Explanation: By default, Pandas assigns integer index values starting from 0.
Q-12
What will be the output of the following statement?
Medium
Pandas Series
Sample Data
s = [10, 20, 30]
Reference Code
import pandas as pd
s = pd.Series([10, 20, 30])
print(s[1])
Answer: 20
Explanation: Index position 1 refers to the second element of the Series, which is 20.
Q-13
How many rows and columns does the DataFrame have?
Medium
Pandas DataFrame
Sample Data
| Name | Maths | Science |
|---|---|---|
| Anu | 80 | 85 |
| Ravi | 75 | 78 |
| Meena | 90 | 88 |
Reference Code
import pandas as pd
df = pd.DataFrame({
'Name': ['Anu', 'Ravi', 'Meena'],
'Maths': [80, 75, 90],
'Science': [85, 78, 88]
})
Answer: 3 rows and 3 columns
Explanation: The DataFrame has 3 rows (students) and 3 columns (Name, Maths, Science).
Q-14
Which column will be returned by the following statement?
Medium
Pandas DataFrame
Sample Data
DataFrame with columns: Name, Age, Marks
Reference Code
print(df['Marks'])
Answer: Only Marks column as Series
Explanation: Accessing a single column using df['column'] returns a Series.
Q-15
Which function is used to display the first five rows of a DataFrame?
Medium
Pandas DataFrame
Answer: head()
Explanation: The head() function displays the first five rows by default.
Q-16
What will be the shape of the DataFrame?
Medium
Pandas DataFrame
Sample Data
| A | B |
|---|---|
| 1 | 4 |
| 2 | 5 |
| 3 | 6 |
| 4 | 7 |
Reference Code
import pandas as pd
df = pd.DataFrame({'A':[1,2,3,4],'B':[4,5,6,7]})
print(df.shape)
Answer: (4, 2)
Explanation: The DataFrame has 4 rows and 2 columns, so shape is (4, 2).
Q-17
Which parameter is used to prevent the index from being written to a CSV file?
Medium
CSV Import Export
Answer: index=False
Explanation: Setting index=False prevents the index from being written to the CSV file.
Q-18
What type of object is returned by read_csv()?
Medium
CSV Import Export
Answer: DataFrame
Explanation: The read_csv() function returns a Pandas DataFrame.
Q-19
Which statement about Pandas Series and NumPy ndarray is correct?
Medium
Pandas vs NumPy
Answer: Only Series supports labeled indexing
Explanation: Pandas Series supports labeled indexing, while NumPy ndarray does not.
Q-20
Which attribute is used to check the data type of elements stored in a Series?
Medium
Pandas Series
Answer: dtype
Explanation: The dtype attribute shows the data type of elements in a Series.
Q-21
What will be the output of the following code?
Hard
Pandas Series
Sample Data
Series values: [5, 10, 15]
Reference Code
import pandas as pd
s = pd.Series([5, 10, 15], index=['a', 'b', 'c'])
print(s['b'])
Answer: 10
Explanation: Label-based indexing is used. Index 'b' corresponds to value 10.
Q-22
What will be returned by the following statement?
Hard
Pandas DataFrame
Sample Data
DataFrame with columns: Name, Marks
Reference Code
print(type(df['Marks']))
Answer: <class 'pandas.core.series.Series'>
Explanation: Accessing a single column using df['Marks'] returns a Pandas Series.
Q-23
What will be the shape of the DataFrame after execution?
Hard
Pandas DataFrame
Sample Data
| A | B | C |
|---|---|---|
| 1 | 4 | 7 |
| 2 | 5 | 8 |
Reference Code
import pandas as pd
df = pd.DataFrame({'A':[1,2],'B':[4,5],'C':[7,8]})
df2 = df[['A','C']]
print(df2.shape)
Answer: (2, 2)
Explanation: Two rows remain and only columns A and C are selected, giving shape (2, 2).
Q-24
What will be the result of the following operation?
Hard
Pandas Series
Sample Data
Series values: [10, 20, 30]
Reference Code
import pandas as pd
s = pd.Series([10, 20, 30])
print(s + 5)
Answer: Each value increased by 5
Explanation: Pandas Series supports vectorized operations; 5 is added to every element.
Q-25
Which parameter ensures that the first column in a CSV file becomes the index?
Hard
CSV Import Export
Answer: index_col=0
Explanation: index_col=0 tells Pandas to use the first column as the index.
Q-26
What happens when a Pandas Series is created from a dictionary?
Hard
Pandas Series
Answer: Dictionary keys become index labels
Explanation: When a Series is created from a dictionary, keys become the index.
Q-27
Which operation will result in alignment based on index labels?
Hard
Pandas Series
Sample Data
s1 index: [a, b, c]
s2 index: [b, c, d]
Reference Code
s3 = s1 + s2
Answer: Index-based alignment before addition
Explanation: Pandas aligns data based on index labels before performing operations.
Q-28
Which statement correctly distinguishes Pandas DataFrame from NumPy ndarray?
Hard
Pandas vs NumPy
Answer: DataFrame supports labeled rows and columns
Explanation: A DataFrame supports both row and column labels, unlike ndarray.
Q-29
Which attribute returns both row and column labels of a DataFrame?
Hard
Pandas DataFrame
Answer: axes
Explanation: The axes attribute returns row index and column labels.
Q-30
Which statement about Pandas Series and NumPy ndarray is TRUE?
Hard
Pandas vs NumPy
Answer: Series supports heterogeneous data types
Explanation: A Pandas Series can store heterogeneous data types, unlike ndarray.
Q-31
What will be the output of the following code?
Medium
Pandas Series
Sample Data
Series values: [2, 4, 6, 8]
Reference Code
import pandas as pd
s = pd.Series([2, 4, 6, 8])
print(s[1:3])
Answer: 4, 6
Explanation: Slicing uses start index inclusive and end index exclusive, so values at positions 1 and 2 are returned.
Q-32
Which type of object is returned by the following expression?
Medium
Pandas DataFrame
Sample Data
DataFrame with columns: Name, Age, Marks
Reference Code
result = df[['Marks']]
Answer: DataFrame
Explanation: Using double square brackets returns a DataFrame, even when selecting a single column.
Q-33
What will be the shape of the DataFrame?
Medium
Pandas DataFrame
Sample Data
| A | B | C |
|---|---|---|
| 1 | 2 | 3 |
| 4 | 5 | 6 |
Reference Code
import pandas as pd
df = pd.DataFrame([[1,2,3],[4,5,6]], columns=['A','B','C'])
print(df.shape)
Answer: (2, 3)
Explanation: The DataFrame has 2 rows and 3 columns.
Q-34
Which method is most suitable to display only the last 3 rows of a DataFrame?
Medium
Pandas DataFrame
Answer: tail(3)
Explanation: tail(3) displays the last three rows of a DataFrame.
Q-35
Which statement correctly describes the output of df.info()?
Medium
Pandas DataFrame
Answer: Data types and non-null counts
Explanation: info() shows column names, data types, and non-null counts.
Q-36
What will be the index of the resulting Series?
Medium
Pandas Series
Sample Data
Original Series index: [a, b, c]
Reference Code
import pandas as pd
s = pd.Series([10, 20, 30], index=['a','b','c'])
print(s[['a','c']])
Answer: ['a', 'c']
Explanation: When selecting by labels, the original index labels are preserved.
Q-37
Which parameter is used to prevent column headers from being written to a CSV file?
Medium
CSV Import Export
Answer: header=False
Explanation: header=False prevents column names from being written to the CSV file.
Q-38
What happens if a Series is created with fewer index labels than values?
Medium
Pandas Series
Answer: Error is raised
Explanation: The number of index labels must match the number of values; otherwise, an error occurs.
Q-39
Which statement about df.axes is correct?
Medium
Pandas DataFrame
Answer: Returns both row and column labels
Explanation: df.axes returns a list containing the index and column labels.
Q-40
Which operation will return a NumPy ndarray from a Pandas object?
Medium
Pandas vs NumPy
Answer: df.values
Explanation: The values attribute returns the underlying NumPy ndarray.
Q-41
What will be the output of the following code?
Hard
Pandas Series
Sample Data
Series s:
Index → a b c
Values → 10 20 30
Reference Code
import pandas as pd
s = pd.Series([10, 20, 30], index=['a','b','c'])
print(s[1])
Answer: 20
Explanation: Integer-based indexing is positional here. Index position 1 refers to the second value, 20.
Q-42
What will be the output of the following code?
Hard
Pandas Series
Sample Data
Series s:
Index → a b c
Values → 10 20 30
Reference Code
import pandas as pd
s = pd.Series([10, 20, 30], index=['a','b','c'])
print(s['b':'c'])
Answer: 20, 30
Explanation: Label-based slicing in Pandas is inclusive of both start and end labels.
Q-43
What will be the shape of the resulting DataFrame?
Hard
Pandas DataFrame
Sample Data
| Name | Maths | Science |
|---|---|---|
| A | 80 | 70 |
| B | 60 | 75 |
| C | 90 | 85 |
Reference Code
import pandas as pd
df = pd.DataFrame({
'Name':['A','B','C'],
'Maths':[80,60,90],
'Science':[70,75,85]
})
df2 = df[['Maths','Science']]
print(df2.shape)
Answer: (3, 2)
Explanation: All three rows remain, and only two columns are selected, giving shape (3, 2).
Q-44
What will be the output of the following operation?
Hard
Pandas Series
Sample Data
Series s1 index: a, b, c
Series s2 index: b, c, d
Reference Code
import pandas as pd
s1 = pd.Series([10,20,30], index=['a','b','c'])
s2 = pd.Series([1,2,3], index=['b','c','d'])
print(s1 + s2)
Answer: Values added only for matching indices
Explanation: Pandas aligns Series by index labels. Only matching indices (b, c) are added; others become NaN.
Q-45
What will be the data type of the object returned?
Hard
Pandas DataFrame
Sample Data
DataFrame df with columns: Name, Marks
Reference Code
result = df.loc[:, 'Marks']
Answer: Series
Explanation: Selecting a single column using .loc returns a Pandas Series.
Q-46
What will be the output of the following code?
Hard
Pandas DataFrame
Sample Data
| A | B |
|---|---|
| 1 | 4 |
| 2 | 5 |
| 3 | 6 |
Reference Code
import pandas as pd
df = pd.DataFrame({'A':[1,2,3],'B':[4,5,6]})
print(df.iloc[1:3, 0])
Answer: 2, 3
Explanation: iloc slicing is positional and end-exclusive. Rows 1 and 2 from column 0 give values 2 and 3.
Q-47
What happens when the following CSV file is read?
Hard
CSV Import Export
Sample Data
CSV file marks.csv:
Roll,Marks
1,80
2,75
Reference Code
import pandas as pd
df = pd.read_csv('marks.csv', index_col=0)
Answer: Roll becomes index
Explanation: index_col=0 makes the first column (Roll) the DataFrame index.
Q-48
Which rows will be displayed by the following statement?
Hard
Pandas DataFrame
Sample Data
| Roll | Marks |
|---|---|
| 1 | 40 |
| 2 | 55 |
| 3 | 70 |
| 4 | 85 |
Reference Code
print(df[df['Marks'] >= 60])
Answer: Roll 3 and 4
Explanation: Boolean indexing selects rows where Marks are 60 or more: Roll 3 and 4.
Q-49
What will be the output of the following code?
Hard
Pandas vs NumPy
Sample Data
Series s:
Values → 10, 20, 30
Reference Code
import pandas as pd
s = pd.Series([10,20,30])
print(s.values)
Answer: NumPy ndarray
Explanation: The values attribute returns the underlying NumPy ndarray.
Q-50
Which statement best explains the behaviour of the following code?
Hard
Pandas Series
Sample Data
Series s with index a, b, c
Reference Code
s = pd.Series([1,2,3], index=['a','b','c'])
print(s + 1)
Answer: Addition happens element-wise using index alignment
Explanation: Pandas supports vectorized operations; scalar addition is applied to each element.