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

GRADE XII - Python Connectivity with MySQL

  Python Connectivity with MySQL In real-life applications, the data inputted by the user and processed by the application needs to be saved permanently, so that it can be used for future manipulation. Usually, input and output data is displayed during execution but not stored, as it resides in main memory , which is temporary — i.e., it gets erased once the application is closed. This limitation can be overcome by sending the data to be stored in a database , which is made accessible to the user through a Front-End interface . Key Concepts Database A database is an organized collection of data that is stored and accessed electronically from a computer system. DBMS (Database Management System) A DBMS is software that interacts with end-users, applications, and the database to capture and analyze data. Front-End The Front-End is the user interface of the application, responsible for input/output interaction with the user. Back-End The Back-End refe...

GRADE XII - CSV FILE

CSV FILE (COMMA SEPERATED VALUE) CSV NOTES LINK :  CSV NOTES - CLICK HERE Writing data to a CSV file involves the following steps: Import the csv module Open the CSV file in write mode ( "w" ) using open() Create the writer object Write the data into the file using the writer object Close the file using close() writerow() Method This method is used to write a single row to a CSV file. It takes a sequence (list or tuple) as its parameter, and writes the items as a comma-separated line in the file. You do not need to add a newline character (\n) — it automatically places the end-of-line marker.     EXAMPLE 1: import csv  # Importing the csv module # CSV file opened using relative path csv_fobj = open("emp.csv", "w")   # Writer object created wtr_obj = csv.writer(csv_fobj) # Record with field heading is written wtr_obj.writerow(["Empno", "Name", "Salary"]) # Records with data are...