You are on page 1of 4

Q.

1) A dictionary grade contains the following data: Grade = {‘name’:[‘Rashmi’, ‘Harsh’, ‘John’, ‘Ram’,
‘Soni’, ‘Vicky’, ‘Tom’], ‘grades’:[‘A1’, ‘A2’, ‘B1’, ‘A1’,’B2’,’A2’, ‘A1’]}

Write the statements for the followings:–

a) Create a dataframe called GR.

b) Find the output of GR.

c) Add a column called percentage with the data (92,89,None,95,68,None,93)

d) Rearrange the columns as name, percentage and grade

CODE:

import pandas as pd

import numpy as np

Grade= {'Name':['Rashmi','Harsh','John' ,'Ram','Soni','Victy','Tom'],'Grades':


['A1','A2','B1','A1','B2','A2','A1']}

GR=pd.DataFrame(Grade)

print(GR)

GR["Percentage"] =[92, 89, None, 95, 68, None, 93]

print(GR)

GR= GR[["Name", "Percentage", "Grades"]]

print(GR)

Q.2) Write the code in pandas to create the following dataframes :

dict1={‘Mark1’:[10,40,15,40],’Mark2’:[15,45,30,70]}

dict2={‘Mark1’:[30,20,20,50],’Mark2’:[20,25,,]}

CODE:

import pandas as pd

import numpy as np

dict1=pd.DataFrame({'mark1':[10,40,15,40],'Mark2':[15,45,30,70]})
dict2=pd.DataFrame({'Mark1':[30,20,20,50],'Mark2':[20,25,np.NaN,np.NaN]})

print(dict1)

print(dict2)

Q.3) Create the following DataFrame Sales containing year wise sales figures for five sales
persons in INR. Use the years as column labels, and sales person names as row labels.

Use the DataFrame created in Question above to do the following:

a. Display the row labels of Sales.


b. Display the column labels of Sales.
c. Display the datatypes of each column of Sales.
d. Display the dimensions, shape, size and values of Sales.
e. Display the last two rows of Sales.
f. Display the first two columns of Sales.
g. Create a dictionary using the following data.
Use this data to create a DataFrame , Sa les2.

h. Change the DataFrame’Sales’such that this becomes its transpose


i. Check if Sales2 is empty or it contains data.
j. Display the sales made by all sales persons in the year 2017.
k. Display the sales2 made by Madhu and Ankit in the year 2018 .
l. Display the sales made by Shruti 2016 from sales dataframe.
m. Add data to Sales for salesman Sumeet where the sales made are
[196.2,37800,52000,78438] in the years [2014,2015, 2016, 2017] respectively.

n. Delete the data for the year 2014 from the DataFrame Sales.
o. Delete the data for salesman Kinshuk from the DataFrame Sales.
p. Change the name of the sales person Ankit to Vivaan and Madhu to Shailesh.
q. Update the sale made by Shailesh in 2017 to 100000.

CODE:

import pandas as pd

import numpy as np

inr2014=[100.5,150.8,200.9,30000,40000]

inr2015=[12000,18000,22000,30000,45000]

inr2016=[20000,50000,70000,100000,125000]

inr2017=[50000,60000,70000,80000,90000]

dict={'2014':inr2014,'2015':inr2015,'2016':inr2016,'2017':inr2017}

label=['Madhu','Kusum','Kinshuk','Ankit','Shruti']

sales=pd.DataFrame(dict, index=label)

print(sales)

print(sales.index)

print(sales.columns)

print(sales.dtypes)
print(sales.ndim)

print(sales.shape)

print(sales.size)

print(sales.values)

print(sales.tail(2))

print(sales[['2014','2015']])

inr2018=[160000,110000,500000,340000,900000]

label=['Madhu','Kusum','Kinshuu','Ankit','Shruti']

dict1={'2018':inr2018}

sales2=pd.DataFrame(dict1,index=label)

print(sales2)

print(sales.T)

print(sales2.empty)

print(sales['2017'])

print(sales2.loc[['Madhu','Ankit'],['2018']])

print(sales.loc[['Shruti'],['2016']])

sales.loc['Sumeet']=[196.2,37800,52000,78438]

print(sales)

print(sales.drop(columns='2014'))

print(sales.drop('Kinshuk',axis=0))

sales.rename(index={'Madhu':'Sailesh','Ankit':'Vivaan'},inplace=True)

print(sales)

sales.iat[0,3]=100000

print(sales)

You might also like