Consider the following Data Frame 'mdf'.
| ROLL NO | Name | English | Hindi | Maths | |
|---|---|---|---|---|---|
| 0 | 1 | Aditya | 23 | 20 | 28 |
| 1 | 2 | Balwant | 18 | 1 | 25 |
| 2 | 3 | Chirag | 27 | 23 | 30 |
| 3 | 4 | Deepak | 11 | 3 | 7 |
| 4 | 5 | Eva | 17 | 21 | 24 |
(a) Write Python statements for the Data Frame 'mdf': (i) To display the records of the students having roll numbers 2 and 3. (ii) To increase the marks of subject Math by 4, for all students. (b) Write Python statement to display the Roll no and Name of all students who secured less than 10 marks in Maths. OR (Option for Part B only) Write Python statement to display the total marks i.e., sum of marks secured in English, Hindi and Maths for all students.
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →Part (a): (i) mdf[mdf['ROLL NO'].isin([2,3])]; (ii) mdf['Maths'] = mdf['Maths'] + 4; (b) mdf[mdf['Maths'] < 10][['ROLL NO','Name']] returns Deepak.
Part (b) alternative: mdf['English'] + mdf['Hindi'] + mdf['Maths'] gives each student's total (71, 44, 80, 21, 62).
The DataFrame mdf (default integer row index 0..4) can be recreated as:
import pandas as pd
data = {
'ROLL NO': [1, 2, 3, 4, 5],
'Name': ['Aditya', 'Balwant', 'Chirag', 'Deepak', 'Eva'],
'English': [23, 18, 27, 11, 17],
'Hindi': [20, 1, 23, 3, 21],
'Maths': [28, 25, 30, 7, 24]
}
mdf = pd.DataFrame(data)
Part (a)
(i) Records of roll numbers 2 and 3. Use Boolean indexing; .isin() tests membership in a list (equivalently (mdf['ROLL NO']==2) | (mdf['ROLL NO']==3)):
print(mdf[mdf['ROLL NO'].isin([2, 3])])
Output:
ROLL NO Name English Hindi Maths
1 2 Balwant 18 1 25
2 3 Chirag 27 23 30
(ii) Increase Maths by 4 for all students. Add 4 to the whole column and assign it back:
mdf['Maths'] = mdf['Maths'] + 4
(b) Roll no and Name where Maths < 10. Filter the rows, then select the two columns (using the original marks, only Deepak's 7 is below 10):
print(mdf[mdf['Maths'] < 10][['ROLL NO', 'Name']])
Output:
ROLL NO Name
3 4 Deepak …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.