Skip to main content

IP - DATAFRAME REVISION - 27 AUG

 

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

Popular posts from this blog

CS - SORTING/SEARCHING ALGORITHMS

  SORTING ALGORITHMS                       SORTING ALGORITHM PDF LINK #Bubble Sort          ·        The outer loop iterates through the entire array. ·        The inner loop compares adjacent elements and swaps them if they are out of order. ·        The outer loop runs n times, and each pass moves the largest element to its correct position. arr=[3,8,5,2,1] n = len(arr) print(n) for i in range(n):  #traverse through all the elements         # Last i elements are already sorted, no need to check them         for j in range(0, n-i-1):              # Swap if the element found is greater than the next element              if arr[j] > arr[j+1]:               ...

GRADE XI - NESTED FOR LOOP

                                                         NESTED FOR LOOP 1. for var1 in range(3):      print(var1,"OUTER LOOP")          # 0 outer loop       1 outer loop      2 outer loop          for var2 in range(2):                  print(var2+1,"INNER LOOP")    #1  2 inner loop     1  2  inner loop   1 2 inner loop  2. Print the following pattern using for loop: 1  1 2  1 2 3  1 2 3 4  Sol: for r in range(1,5):   #ROWS     for c in range(1,r+1):   #COLUMNS         print(c,end=" ")     print() 3. Print the following pattern using for loop: @  @ @  @ @ @...