Solutions – Class XII – Informatics Practices
Max Marks: 25 Topic: DATAFRAME Time: 35 minutes
SECTION A [5 × 1 = 5]
1. Name the Pandas object that stores two-dimensional labeled data.
Answer: DataFrame
2. Correct the error:
data = [{‘a’:1,’b’:2}, {‘a’:3,’b’:4}]
df = pd.DataFrame(data)
df(‘a’) = [10,20]
Answer: df['a'] = [10,20]
3. Which of the following gives the last 3 rows of a DataFrame `df`?
a) df.head(3) b) df.tail(3) c) df.last(3) d) df.end(3)
Answer: b) df.tail(3)
4. What is the correct way to access column 'A' from a DataFrame `df`?
a) df['A'] b) df.A c) Both a and b d) df->A
Answer: c) Both a and b
5. Assertion and Reasoning:
Assertion (A): A DataFrame can hold heterogeneous data types.
Reason (R): Pandas is not capable of handling large datasets efficiently.
Choose the correct option.
Answer: A is true, R is false
SECTION B [1 × 2 = 2]
6. Create a DataFrame to store roll number, marks in English and marks in Computer for 4 students.
Answer:
import pandas as pd
data = {
'Roll No': [1, 2, 3, 4],
'English': [85, 78, 90, 88],
'Computer': [95, 82, 91, 89]
}
df = pd.DataFrame(data)
print(df)
SECTION C [3 × 3 = 9]
7. Predict the output:
Answer:
x y
0 1.0 2.0
1 3.0 NaN
2 NaN 4.0
8. Create a DataFrame using dictionary of Series with columns: Math, Science for 3 students.
Answer:
import pandas as pd
math = pd.Series([80, 85, 90], index=['S1', 'S2', 'S3'])
science = pd.Series([78, 82, 88], index=['S1', 'S2', 'S3'])
data = {'Math': math, 'Science': science}
df = pd.DataFrame(data)
print(df)
9. Consider the sales data...
Answer:
i) Code:
import pandas as pd
data = {
'Shop': ['Mart1', 'Mart2', 'Mart3'],
'Q1': [200, 190, 230],
'Q2': [210, 215, 220],
'Q3': [250, 205, 240],
'Q4': [300, 280, 260]
}
df = pd.DataFrame(data)
print(df)
ii) Output:
a) (3, 5)
b) Output:
Q1 Q2
0 200 210
1 190 215
2 230 220
SECTION D [1 × 4 = 4]
10. Given the DataFrame `inventory`:
Answer:
a) inventory.loc[:, ['Qty', 'Rate']]
b) inventory.iloc[:, [0, 2]]
c) inventory.loc[['Apple', 'Guava']]
d) inventory.iloc[[1, 3]]
SECTION E [1 × 5 = 5]
11. Given the DataFrame...
Answer:
i) Index(['City', 'Temp', 'Humidity'], dtype='object')
ii) City Mumbai
Temp 30
Humidity 65
Name: C2, dtype: object
iii) Output:
C1 Delhi
C2 Mumbai
C3 Chennai
Name: City, dtype: object
iv) 54.0
v) Output:
df1.City
C1 Delhi
C2 Mumbai
C3 Chennai
C4 Kolkata
C5 Jaipur
Name: City, dtype: object