WRITE A PROGRAM TO ITERATE OVER A DATAFRAME CONTAINING NAMES AND MARKS, WHICH THEN CALCULATE MARKS AS PER THE CRITERIA AND THEN ADDS THEM TO THE GRADE COLUMN.
MARKS >= 80 GRADE IS A, MARKS >= 60 GRADE IS B, MARKS < 60 GRADE IS C
Solution :-
import pandas as pd import numpy as np names = pd.Series([‘Rohan’,’Samay’,’Shanatanu’,’Shruti’,’Tanya’]) marks=pd.Series([89,76,43,38,98]) stu ={‘name’:names,’marks’:marks} df1=pd.DataFrame(stu,columns=[‘name’,’marks’]) df1[‘grade’]=np.nan print(“initial values:\n”,df1) for(col,colSeries) in df1.iteritems(): length=len(colSeries) if col==’marks’: lstmrks=[] for row in range(length): mrks=colSeries[row] if mrks>=80: lstmrks.append(‘A’) elif mrks>=60: lstmrks.append(‘B’) elif mrks<60: lstmrks.append(‘C’) df1[‘grade’]=lstmrks print(“\n Dataframe after calculating grades:- “) print(df1)