DATAFRAME REVISION
GRADE XII
(a) Write python statements to create a data frame “STAFF” for the following data.
Name Age Designation
T100 YAMINI 35 PRINCIPAL
T101 DINESH 40 SYSTEM MANAGER
T102 SHYAM 50 TEACHER
T103 VINOD 45 ACCOUNTANT
T104 SYRIA 30 RECEPTIONIST
(b) Write the python code to rename the column designation to desig in the
dataframe created in the previous question.
(c) Add one more student‟s record permanently in the dataframe.
(d) Add one more column to store the fee details.
(e) Write python code to delete column fee of data frame permanently.
(f) Write python code to delete the 3rd and 5th rows from dataframe.
(g) Write a python code to display the name and designation of employees whose
age more than 40 and less than 50.
(h) Change the designation of Shyam to Vice Principal
(i) Display the details of teachers having the index as T101, T103.
SOL:
Ans: (a) import pandas as pd
data={'name':['Yamini','Dinesh','Shyam','Vinod','Syria'],'Age':[35,40,50,45,30],'design
ation':['Principal','System Manager','Teacher','Accountant','Receptionist']}
staff=pd.DataFrame(data,index=['T100','T101','T102','T103','T104'])
print(staff)
(b)staff.rename(columns={'designation':'desig'})
Note: use inplace=True, if you want permanent renaming
(c)staff.loc['T105']=['Ram',15,'Student']
(d)staff['fees']=[6000,7000,4500,4600,4900,12000]
(e) staff.drop(columns='fees',inplace=True)
(f)staff.drop(df.index[[2,4]])
(g)staff.loc[(staff.Age>40)&(staff.Age<50),['name','designation']]
(h)staff.loc[staff.name=='Shyam','desig']='Vice Principal'
(i)staff.loc[['T101','T103']]
Comments
Post a Comment