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']]


DATA ENTRY USING DATAFRAME

#

import pandas as pd

#eval() PARSES THE EXPRESSION PASSES TO IT AND RUN CODE WITH IN THE EXPRESSION

roll=eval(input("Enter the 3 rollnos:"))

name=eval(input("Enter the 3 Names:"))

marks=eval(input("Enter the 3 Marks:"))


d={"ROLLNO":roll,"NAME":name,"MARKS":marks}

df=pd.DataFrame(d)

print(df)

#sorting

#IN ACENDING ORDER

print("#"*50)

df=df.sort_values(by=['MARKS'])

print(df)

print("#"*50)

df=df.sort_values(by=['MARKS'],ascending=[False])

print(df)

'''


l=[['Alex',10],['bob',12],['Clark',13]]

data1=pd.DataFrame(l,columns=['NAME','AGE'])

print(data1)

#data1.iloc[1,1] = 4

data1.loc[2, 'AGE'] = 34

#data1.at[0,'Age']= 20


Comments

Popular posts from this blog

PYTHON - MYSQL CONNECTIVITY CODE

  #INSERTION OF DATA import mysql.connector mydb = mysql.connector.connect( host="localhost", user="root", passwd="root", database="school" ) print("Successfully Connected") #print(mydb) mycursor=mydb.cursor()   v1=int(input("enter ID:")) v2=input("enter name:") v3=input("enter Gender:") v4=int(input("enter age:")) sql='insert into TEACH values("%d","%s","%s","%s")'%(v1,v2,v3,v4) print(sql) mycursor.execute(sql) mydb.commit() print("record added") #MYSQL Connection code – Deletion on database SOURCE CODE: s=int(input("enter id of TEACHER to be deleted:")) r=(s,) v="delete from TEACH where id=%s" mycursor.execute(v,r) mydb.commit() print("record deleted") MYSQL Connection code – Updation on database SOURCE CODE: import mysql.connector mydb = mysql.connector.c...

MySQL - ASSIGNMENT | 10th March 25

  MySQL DATABASE QUESTIONS GRADE XII 1.      What is a database? Name any two RDBMS softwares. 2.      Discuss features of SQL. 3.      Differentiate between ALTER and UPDATE statements. 4.      Elucidate the terms degree and cardinality of a relation. 5.      Answer the following questions on the basis of the given table: Admno             Name               Subject            Gender             Average 1001                 Amit                 Math         ...