English

Can you write a shortened code to get the output of the program? Name Raman Raman Raman UT 1 2 3 Maths 22 21 14 Maths 22 21 14 S.St 18 17 15

Advertisements
Advertisements

Question

Can you write a shortened code to get the output of the program?

  Name UT Maths Science S.St Hindi Eng
0 Raman 1 22 21 18 20 21
1 Raman 2 21 20 17 22 24
2 Raman 3 14 19 15 24 23

>>> dfRaman[['Maths','Science','S. St','Hindi','Eng']].sum()

Maths 57
Science 60
S.St 50
Hindi 66
Eng 68
dtype int64

>>> dfRaman[['Maths','Science','S. St','Hindi','Eng']].sum(axis=1)

0 102
1 104
2 95
dtype int64
Code Writing
Advertisements

Solution

import pandas as pd
df = pd.DataFrame({
    'Name':['Raman','Raman','Raman'],
    'UT':[1,2,3],
    'Maths':[22,21,14],
    'Science':[21,20,19],
    'S.St':[18,17,15],
    'Hindi':[20,22,24],
    'Eng':[21,24,23]
})
dfRaman = df[df['Name']=='Raman']
print("Marks obtained by Raman in each test are:")
print(dfRaman)
print("\nTotal marks obtained by Raman in each subject:")
print(dfRaman[['Maths','Science','S.St','Hindi','Eng']].sum())
print("\nTotal marks scored by Raman in each test:")
print(dfRaman[['Maths','Science','S.St','Hindi','Eng']].sum(axis=1))
shaalaa.com
  Is there an error in this question or solution?
Chapter 3: Data Handling using Pandas - II - Intext Questions [Page 69]

APPEARS IN

NCERT Informatics Practices [English] Class 12
Chapter 3 Data Handling using Pandas - II
Intext Questions | Q 1. | Page 69
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×